使用dplyr将多个列合并在一起

时间:2017-04-22 13:31:10

标签: r dplyr reshape

我正在尝试使用大约100列和100000行的数据帧,并将类似命名的列合并到一列中,例如C1,C2,C3进入C.我一直在尝试使用dplyr收集功能,但我似乎无法获得所需的输出我很乐意使用另一个包,如果这更容易。我在下面列出了一个简化示例。我确信我错过了一些简单的东西,任何帮助都会非常感谢。

id = c(222, 222, 222, 333, 333, 333, 444, 444, 444) 
timepoint = c("aa", "aa", "bb", "aa", "aa", "bb", "aa", "aa", "bb") 
position = c(1, 2, 1, 1, 2,1 , 1, 2, 1)
C1 = c("aat", "aaf", "bbg", "aag", "aag", "bbg", "aag", "aag", "bbg") 
P1 = c("A", "B", "C", "J", "J", "J", "J", "H", "H") 
X1 = c(21, 22, 23, 33, 35, 33, 41, 43, 45) 
C2 = c("aat", "aaf", "bbg", "aag", "aag", "bbg", "aag", "aag", "bbg") 
P2 = c("A", "B", "C", "J", "J", "J", "J", "H", "H") 
X2 = c(21, 22, 23, 33, 35, 33, 41, 43, 45) 
C3 = c("aat", "aaf", "bbg", "aag", "aag", "bbg", "aag", "aag", "bbg") 
P3 = c("A", "B", "C", "J", "J", "J", "J", "H", "H") 
X3 = c(21, 22, 23, 33, 35, 33, 41, 43, 45) 

df = data.frame(id, timepoint, position, C1, P1, X1, C2, P2, X2, C3, P3, X3)

我想从这种格式出发

id  timepoint   position    C1  P1  X1  C2  P2  X2  C3  P3  X3
222 aa          1           aat A   21  aat A   21  aat A   21
222 aa          2           aaf B   22  aaf B   22  aaf B   22
222 bb          1           bbg C   23  bbg C   23  bbg C   23
333 aa          1           aag J   33  aag J   33  aag J   33
333 aa          2           aag J   35  aag J   35  aag J   35
333 bb          1           bbg J   33  bbg J   33  bbg J   33
444 aa          1           aag J   41  aag J   41  aag J   41
444 aa          2           aag H   43  aag H   43  aag H   43
444 bb          1           bbg H   45  bbg H   45  bbg H   45

这种格式。

id  timepoint   position    C   P   X
222 aa          1           aat A   21
222 aa          2           aaf B   22
222 bb          1           bbg C   23
333 aa          1           aag J   33
333 aa          2           aag J   35
333 bb          1           bbg J   33
444 aa          1           aag J   41
444 aa          2           aag H   43
444 bb          1           bbg H   45
222 aa          1           aat A   21
222 aa          2           aaf B   22
222 bb          1           bbg C   23
333 aa          1           aag J   33
333 aa          2           aag J   35
333 bb          1           bbg J   33
444 aa          1           aag J   41
444 aa          2           aag H   43
444 bb          1           bbg H   45
222 aa          1           aat A   21
222 aa          2           aaf B   22
222 bb          1           bbg C   23
333 aa          1           aag J   33
333 aa          2           aag J   35
333 bb          1           bbg J   33
444 aa          1           aag J   41
444 aa          2           aag H   43
444 bb          1           bbg H   45

1 个答案:

答案 0 :(得分:2)

我们可以使用melt中的data.table轻松完成此操作,这可能需要多个measure patterns

library(data.table)
melt(setDT(df), measure = patterns("^C\\d+", "^P\\d+", "^X\\d+"), 
           value.name = c("C", "P", "X"))[, variable := NULL][]
#     id timepoint position   C P  X
# 1: 222        aa        1 aat A 21
# 2: 222        aa        2 aaf B 22
# 3: 222        bb        1 bbg C 23
# 4: 333        aa        1 aag J 33
# 5: 333        aa        2 aag J 35
# 6: 333        bb        1 bbg J 33
# 7: 444        aa        1 aag J 41
# 8: 444        aa        2 aag H 43
# 9: 444        bb        1 bbg H 45
#10: 222        aa        1 aat A 21
#11: 222        aa        2 aaf B 22
#12: 222        bb        1 bbg C 23
#13: 333        aa        1 aag J 33
#14: 333        aa        2 aag J 35
#15: 333        bb        1 bbg J 33
#16: 444        aa        1 aag J 41
#17: 444        aa        2 aag H 43
#18: 444        bb        1 bbg H 45
#19: 222        aa        1 aat A 21
#20: 222        aa        2 aaf B 22
#21: 222        bb        1 bbg C 23
#22: 333        aa        1 aag J 33
#23: 333        aa        2 aag J 35
#24: 333        bb        1 bbg J 33
#25: 444        aa        1 aag J 41
#26: 444        aa        2 aag H 43
#27: 444        bb        1 bbg H 45