通过在r中匹配ID来改变数据帧
我有三个数据帧: df1: df2: df3: 我想将这三个分数变为这样的数据帧,使用NA表示缺少的值通过在r中匹配ID来改变数据帧,r,plyr,dplyr,mutated,R,Plyr,Dplyr,Mutated,我有三个数据帧: df1: df2: df3: 我想将这三个分数变为这样的数据帧,使用NA表示缺少的值 id score1 score2 score3 1 50 33 50 2 23 23 23 3 40 NA 40 4 68 64 68 5 82 12 82 6 38 32 NA 或者像这样删除NA值: id score1 score2 score3 1 50
id score1 score2 score3
1 50 33 50
2 23 23 23
3 40 NA 40
4 68 64 68
5 82 12 82
6 38 32 NA
或者像这样删除NA值:
id score1 score2 score3
1 50 33 50
2 23 23 23
4 68 64 68
5 82 12 82
然而,突变(在dplyer中)不会采用不同的长度。所以我不能变异。我怎么做?你可以试试
Reduce(function(...) merge(..., by='id'), list(df1, df2, df3))
# id score1 score2 score3
#1 1 50 33 50
#2 2 23 23 23
#3 4 68 64 68
#4 5 82 12 82
如果有多个数据集对象名,其模式为“df”,后跟数字
Reduce(function(...) merge(..., by='id'), mget(paste0('df',1:3)))
或者,您可以使用@DavidArenburg评论的
ls(pattern='df\\d+')
代替paste0('df',1:3)
,谢谢@阿克伦。我以前不知道“reduce”函数:)
id score1 score2 score3
1 50 33 50
2 23 23 23
4 68 64 68
5 82 12 82
Reduce(function(...) merge(..., by='id'), list(df1, df2, df3))
# id score1 score2 score3
#1 1 50 33 50
#2 2 23 23 23
#3 4 68 64 68
#4 5 82 12 82
Reduce(function(...) merge(..., by='id'), mget(paste0('df',1:3)))