0

我有三个数据框:

df1:

id score1
1   50
2   23
3   40
4   68
5   82
6   38

df2:

id score2
1   33
2   23
4   64
5   12
6   32

df3:

id score3
1   50
2   23
3   40
4   68
5   82

我想将三个分数变异为这样的数据框,使用 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

但是,mutate(在 dplyer 中)不会采用不同的长度。所以我不能变异。我怎样才能做到这一点?

4

1 回答 1

2

你可以试试

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

或者paste0('df', 1:3),您可以ls(pattern='df\\d+')按照@DavidArenburg 的评论使用

于 2015-03-10T17:22:45.377 回答