結合我有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"))
我想列hello
,world
和foo
在單一組合柱,由countrycode
和year
索引,如下:
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"))
我從試圖從標準的R既cbind
gather
和包,並且都不起作用。