Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过在r中匹配ID来改变数据帧_R_Plyr_Dplyr_Mutated - Fatal编程技术网

通过在r中匹配ID来改变数据帧

通过在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

我有三个数据帧:

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     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)))