小编kna*_*und的帖子

如何组合R中相同数据帧的不同字符列

我在R中有一个数据框如下:

D = data.frame(countrycode = c(2, 2, 2, 3, 3, 3), 
           year = c(1980, 1991, 2013, 1980, 1991, 2013), 
           hello = c("A", "B", "C", "D", "E", "F"), 
           world = c("Z", "Y", "X", "NA", "Q", "NA"), 
           foo = c("Yes", "No", "NA", "NA", "Yes", "NA"))
Run Code Online (Sandbox Code Playgroud)

我想要列hello,worldfoo合并在一个列中,索引为countrycodeyear,如下所示:

output<-data.frame(countrycode=c(2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
    year=c(1980,1980,1980,1991,1991,1991,2013,2013,2013,1980,1980,1980,1991,1991,1991,2013,2013,2013),
    Combined=c("A","Z","Yes","B","Y","No","C","X","NA","D","NA","NA","E","Q","Yes","F","NA","NA"))
Run Code Online (Sandbox Code Playgroud)

我都一直cbind从标准R和gathertidyr包装,既不似乎工作.

r

1
推荐指数
1
解决办法
87
查看次数

标签 统计

r ×1