R中的Data.frame:两列的连接,数字而不是出现

时间:2016-11-08 11:33:09

标签: r dataframe data-manipulation

我有一个非常简单的问题,但我找不到答案:我有这个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$ba$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

1 个答案:

答案 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"