合并不同的数据帧列以创建新列表,然后创建列表

时间:2015-06-03 11:19:37

标签: r list merge dataframe

我有两个数据框,其中包含全局列ID

DF1:

structure(list(ID = c("ILMN_1343291", "ILMN_1343292", "ILMN_1343293", 
"ILMN_1343294", "ILMN_1651209", "ILMN_1651217"), sample1 = c(15.8694849642673, 
11.012283668738, 14.995174021271, 15.5837972369044, 8.08964880277197, 
7.83406791970529), sample2 = c(15.8286939941996, 10.8407625194439, 
14.8818391783215, 15.5951964773238, 8.07397790649028, 7.85669590957356
), sample3 = c(15.8694849642673, 11.453825129904, 14.764465720783, 
15.6917233494908, 8.12884795713027, 7.86320578481668), sample4 = c(15.8694849642673, 
13.1291568945281, 14.8250076629948, 15.7193468523012, 8.07217210194779, 
7.84280084254162), sample5 = c(15.8694849642673, 10.223217362866, 
14.9595743905588, 15.362353159395, 8.12336672246356, 7.80055774746275
), sample6 = c(15.8694849642673, 11.3892037666505, 15.3837334429764, 
15.65923530912, 8.05738048722836, 7.81925084187836)), .Names = c("ID", 
"sample1", "sample2", "sample3", "sample4", "sample5", "sample6"
), class = "data.frame", row.names = c(NA, 6L))

DF2:

structure(list(ID = c("ILMN_1343291", "ILMN_1343292", "ILMN_1343293", 
"ILMN_1343294", "ILMN_1651209", "ILMN_1651217"), PVALUE1 = c(0, 
0, 0, 0, 0, 0.4675325), PVALUE2 = c(0, 0, 0, 0, 0, 0.3290415), 
    PVALUE3 = c(0, 0, 0, 0, 0, 0.3074713), PVALUE4 = c(0, 0, 
    0, 0, 0, 0.3543759), PVALUE5 = c(0, 0, 0, 0, 0, 0.6465517
    ), PVALUE6 = c(0, 0, 0, 0, 0.001445087, 0.5390173)), .Names = c("ID", 
"PVALUE1", "PVALUE2", "PVALUE3", "PVALUE4", "PVALUE5", "PVALUE6"
), class = "data.frame", row.names = c(NA, 6L))

期望的输出:

  [[1]]
      ID           sample1   PVALUE1
    1 ILMN_1343291 15.869485 0.0000000
    2 ILMN_1343292 11.012284 0.0000000
    3 ILMN_1343293 14.995174 0.0000000
    4 ILMN_1343294 15.583797 0.0000000
    5 ILMN_1651209  8.089649 0.0000000
    6 ILMN_1651217  7.834068 0.4675325
[[2]]
  ID           sample2   PVALUE2
1 ILMN_1343291 15.828694 0.0000000
2 ILMN_1343292 10.840763 0.0000000
3 ILMN_1343293 14.881839 0.0000000
4 ILMN_1343294 15.595196 0.0000000
5 ILMN_1651209  8.073978 0.0000000
6 ILMN_1651217  7.856696 0.3290415

[[3]]
  ID           sample3   PVALUE3
1 ILMN_1343291 15.869485 0.0000000
2 ILMN_1343292 11.453825 0.0000000
3 ILMN_1343293 14.764466 0.0000000
4 ILMN_1343294 15.691723 0.0000000
5 ILMN_1651209  8.128848 0.0000000
6 ILMN_1651217  7.863206 0.3074713

[[4]]
  ID           sample4   PVALUE4
1 ILMN_1343291 15.869485 0.0000000
2 ILMN_1343292 13.129157 0.0000000
3 ILMN_1343293 14.825008 0.0000000
4 ILMN_1343294 15.719347 0.0000000
5 ILMN_1651209  8.072172 0.0000000
6 ILMN_1651217  7.842801 0.3543759

[[5]]
  ID           sample5   PVALUE5
1 ILMN_1343291 15.869485 0.0000000
2 ILMN_1343292 10.223217 0.0000000
3 ILMN_1343293 14.959574 0.0000000
4 ILMN_1343294 15.362353 0.0000000
5 ILMN_1651209  8.123367 0.0000000
6 ILMN_1651217  7.800558 0.6465517

[[6]]
  ID           sample6     PVALUE6
1 ILMN_1343291 15.869485 0.000000000
2 ILMN_1343292 11.389204 0.000000000
3 ILMN_1343293 15.383733 0.000000000
4 ILMN_1343294 15.659235 0.000000000
5 ILMN_1651209  8.057380 0.001445087
6 ILMN_1651217  7.819251 0.539017300

我想合并df1df2的不同列,然后创建所有数据框的列表。我想将df1df2的第二列合并为ID,这是两个数据框中的第一列并存储在列表中,然后合并df1的第三列, df2存储在列表中,依此类推,直到完成所有列。我知道我可以使用merge()合并两个数据框并使用list()来存储。这里我需要在循环中执行此操作。如何使用最少的代码来完成它,否则我需要写一个很长的代码来实现这一切。谢谢 。

1 个答案:

答案 0 :(得分:1)

这是一次尝试

Res <- Map(function(x, y) cbind(df1[1], x, y), df1[-1], df2[-1])
tempnames <- rbind.data.frame(names(df1)[-1], names(df2)[-1])
Map(function(x, y) `names<-`(x, c("ID", as.character(y))), Res, tempnames)
# $sample1
#             ID   sample1   PVALUE1
# 1 ILMN_1343291 15.869485 0.0000000
# 2 ILMN_1343292 11.012284 0.0000000
# 3 ILMN_1343293 14.995174 0.0000000
# 4 ILMN_1343294 15.583797 0.0000000
# 5 ILMN_1651209  8.089649 0.0000000
# 6 ILMN_1651217  7.834068 0.4675325
# 
# $sample2
#             ID   sample2   PVALUE2
# 1 ILMN_1343291 15.828694 0.0000000
# 2 ILMN_1343292 10.840763 0.0000000
# 3 ILMN_1343293 14.881839 0.0000000
# 4 ILMN_1343294 15.595196 0.0000000
# 5 ILMN_1651209  8.073978 0.0000000
# 6 ILMN_1651217  7.856696 0.3290415
...