我有一个非常简单的问题,但我找不到答案:我有这个data.frame:
b=c("a","a","a","a","a","b","b","b","b","c")
c=c("b","b","b","b","b","c","c","c","c","d")
a<-data.frame(b,c)
为什么我要将a$b
和a$c
向量放在一列中:
f<-c(a$b,a$c)
结果不像
> f<-c(b,c)
> f
[1] "a" "a" "a" "a" "a" "b" "b" "b" "b" "c" "b" "b" "b" "b" "b" "c" "c" "c" "c" "d"
但
> f<-c(a$b,a$c)
> f
[1] 1 1 1 1 1 2 2 2 2 3 1 1 1 1 1 2 2 2 2 3
? 提前谢谢!
编辑: 试过这个,看看上面可能的重复建议:
> a<-data.frame(z=as.character(b),k=as.character(c))
> f<-c(a$z,a$k)
> f [1] 1 1 1 1 1 2 2 2 2 3 1 1 1 1 1 2 2 2 2 3
答案 0 :(得分:2)
您必须在stringsAsFactors
功能中将FALSE
设置为data.frame()
。否则,字符串将被解释为因子,从而导致您的意外输出。
b <- c("a","a","a","a","a","b","b","b","b","c")
c <- c("b","b","b","b","b","c","c","c","c","d")
a <- data.frame(b,c, stringsAsFactors = F)
f <- c(a$b,a$c)
f
[1] "a" "a" "a" "a" "a" "b" "b" "b" "b" "c" "b" "b" "b" "b" "b" "c" "c" "c" "c" "d"