2016-11-08 62 views
0

如何以下列方式组合数据框的列?R(dplyr,tidyr):组合/聚合相关列

data <- data.frame(user.A = c(2,4,6), 
       user.B = c(11,13,15), 
       other.A = c(102,104,106), 
       other.B = c(201,103,105), 
       id = c('001', '004', '006')) 
data 
    user.A user.B other.A other.B id 
1  2  11  102  201 001 
2  4  13  104  103 004 
3  6  15  106  105 006 

# Desired output. 
    user other id 
1 2 102 001 
2 11 201 001 
3 4 104 004 
4 13 103 004 
5 6 106 006 
6 15 105 006 

我相信这可以dyplrtidyr来完成。 dplyr中的bind_rows函数做了类似的操作,但不会创建此期望的输出。

回答

2

使用meltdata.table可以更容易,因为它可能需要多个measurepatterns

library(data.table) 
melt(setDT(data), measure = patterns("^user", "^other"), 
     value.name = c("user", "other"))[, variable := NULL][] 
# id user other 
#1: 001 2 102 
#2: 004 4 104 
#3: 006 6 106 
#4: 001 11 201 
#5: 004 13 103 
#6: 006 15 105 

由于 '用户', '其他' 列numeric,我们还可以用gather/spreadtidyr

library(dplyr) 
library(tidyr) 
gather(data, var, val, -id) %>% 
     separate(var, into = c("var1", "var2")) %>% 
     spread(var1, val) %>% 
     select(-var2) 
# id other user 
#1 001 102 2 
#2 001 201 11 
#3 004 104 4 
#4 004 103 13 
#5 006 106 6 
#6 006 105 15 
2

可以使用reshape功能的变化如下:

new_data <- reshape(data, varying = 1:4, direction = "long") 

varying参数是使用d指定要在哪些列上进行透视。

+0

对于'reshape()'所有的仇恨基地,它有时真的很聪明。 – thelatemail