简体   繁体   English

R减少/合并data.frame中的列

[英]R reduce/combine columns in a data.frame

I'm working with a dataframe and would like to merge two columns into one, if two of the cells have the same value. 我正在使用一个数据框,如果两个单元格具有相同的值,我想将两列合并为一个。

  X1 X2 X3 X4 X5 X6 X7 X8  X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24
1 20 30 40 54 64 74 88 98 108 122 132 142 168 178 188 202 212 222 236 246 256 270 280 290
5  2  4  6  2  4  6  2  4   6   2   4   6   2   4   6   2   4   6   2   4   6   2   4   6
6  6  6  6 13 13 13 20 20  20  27  27  27   6   6   6  13  13  13  20  20  20  27  27  27

Thats the dataset I have. 那就是我拥有的数据集。 I would like to merge two columns if the elements in row 5,6 are the same by combining what ever is in row 1, eg the above dataset would become 如果要合并第1行中的所有内容,则想合并第5,6行中的元素相同的两列,例如,上面的数据集将变为

   X1       X2       X3       X4       X5       X6       X7       X8       X9        X10       X11       X12 
1 (20,168) (30,178) (40,188) (54,202) (64,212) (74,222) (88,236) (98,246) (108,256) (122,270) (132,280) (142,290)
5  2        4        6        2        4        6        2        4        6         2         4         6
6  6        6        6        13       13       13       20       20       20        27        27        27

Thanks in advance EDIT: changed rows to column 在此先感谢编辑:将行更改为列

We could create a grouping variable ('gr') by paste ing the 2nd and 3rd rows. 我们可以通过paste第二行和第三行来创建分组变量(gr)。 Then, we split the sequence of column with 'gr' to get a list output. 然后,我们用'gr' split列的顺序以获取list输出。 Subset the first row of 'df1' based on the column index, paste the elements ( toString is a convenient wrapper for paste(., collapse=', ') , and to add some extra formatting with parentheses we can either use paste or sprintf ), unsplit and assign the output to the first row of 'df1' 根据列索引对'df1'的第一行进行分组, paste元素( toStringpaste(., collapse=', ')的便捷包装器,并添加带有括号的其他格式,我们可以使用pastesprintf ), unsplit并将输出分配给'df1'的第一行

gr <-  paste(df1[2,], df1[3,])

lst <- split(seq_along(df1), gr)
df1[1,] <- unsplit(lapply(lst, function(x)
                 sprintf('(%s)', toString(df1[1,x]))) , gr)
df1
#        X1        X2        X3        X4        X5        X6        X7
#1 (20, 168) (30, 178) (40, 188) (54, 202) (64, 212) (74, 222) (88, 236)
#5         2         4         6         2         4         6         2
#6         6         6         6        13        13        13        20
#         X8         X9        X10        X11        X12       X13       X14
#1 (98, 246) (108, 256) (122, 270) (132, 280) (142, 290) (20, 168) (30, 178)
#5         4          6          2          4          6         2         4
#6        20         20         27         27         27         6         6
#        X15       X16       X17       X18       X19       X20        X21
#1 (40, 188) (54, 202) (64, 212) (74, 222) (88, 236) (98, 246) (108, 256)
#5         6         2         4         6         2         4          6
#6         6        13        13        13        20        20         20
#         X22        X23        X24
#1 (122, 270) (132, 280) (142, 290)
#5          2          4          6
#6         27         27         27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM