gpt4 book ai didi

rbind char 向量到数据框

转载 作者:行者123 更新时间:2023-12-01 07:58:31 25 4
gpt4 key购买 nike

> test <- data.frame()
> test<-rbind(test,c("hi","i","am","bob"))
> test<-rbind(test,c("hi","i","am","alice"))
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "alice") :
invalid factor level, NAs generated

为什么这个最小的例子会产生这个错误?我想将几个字符串行附加到一个空数据框中。

最佳答案

您可以将信息存储在字符矩阵中。当然,您可以使用 as.data.frame 将此矩阵转换为数据框。和论点 stringsAsFactors = FALSE .

> test <- matrix(c("hi","i","am","bob"), nrow = 1)
> test <- rbind(test, c("hi","i","am","alice"))
> test
[,1] [,2] [,3] [,4]
[1,] "hi" "i" "am" "bob"
[2,] "hi" "i" "am" "alice"

> testDF <- as.data.frame(test, stringsAsFactors = FALSE)
> testDF <- rbind(testDF, c("hi","i","am","happy"))
> testDF
V1 V2 V3 V4
1 hi i am bob
2 hi i am alice
3 hi i am happy

关于rbind char 向量到数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com