使用rbind两个组合两个单列变量

时间:2018-04-20 04:58:09

标签: r rbind

我正在尝试简单地对两个列进行处理,并使用以下内容(所有变量都是城市名称,r将其视为因子)

firstcitynames <- rcffull$X1CityName 
secondcitynames <- rcffull$X2CityName
allcitynames <- rbind(firstcitynames, secondcitynames)
allcitynames

然后当我到达View(allcitynames)时,我得到的是一堆数字而不是名字:

[,2276] [,2277] [,2278] [,2279] [,2280] [,2281]
                [,2282] [,2283] [,2284] [,2285] [,2286] [,2287]

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您需要将因子转换为as.character(df$var)

的字符

这是一个插图

a <- factor(letters[1:10])
b <- factor(LETTERS[1:10])
rbind(a,b)
##    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## a    1    2    3    4    5    6    7    8    9    10
## b    1    2    3    4    5    6    7    8    9    10

rbind(as.character(a), as.character(b))
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] "a"  "b"  "c"  "d"  "e"  "f"  "g"  "h"  "i"  "j"  
## [2,] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"  

答案 1 :(得分:0)

假设firstcitynamessecondcitynames属于类型因素 你可以试试这个

rbind(levels(firstcitynames),levels(secondcitynames))

答案 2 :(得分:0)

这个也有效:

firstcitynames <- as.tibble(rcffull$X1CityName)   
secondcitynames <- as.tibble(rcffull$X2CityName)
allcitynames <- rbind(firstcitynames, secondcitynames)
allcitynames