将具有相同ID的数据折叠为单行

时间:2015-11-25 12:35:44

标签: r

我的数据框如下所示:

  B0022HOME_NO  Device_Detail
1      1043036  Laptop/Netbook
2      1043036         Tablet
3      3753259  Laptop/Netbook   
4      3753259  Laptop/Netbook   
5      3753259         Tablet   
6      3753259         Tablet

我想要得到的是这样的,其中每个唯一的B0022HOME_NO值都包含一行,其中包含所有Device_Detail个列表。

  B0022HOME_NO  Device_Detail1  Device_Detail2  Device_Detail3  Device_Detail4
1      1043036  Laptop/Netbook  Tablet
2      3753259  Laptop/Netbook  Laptop/Netbook  Tablet          Tablet         

我对Dplyr(和R)很新,所以一直试图使用gather函数,但无济于事。

1 个答案:

答案 0 :(得分:3)

我们可以尝试

library(data.table)
setDT(df2)[, N:= paste0('Device_Detail',1:.N), B0022HOME_NO]
dcast(df2, B0022HOME_NO~ N, value.var='Device_Detail', fill='')
# B0022HOME_NO Device_Detail1 Device_Detail2 Device_Detail3 Device_Detail4
#1:      1043036 Laptop/Netbook         Tablet                              
#2:      3753259 Laptop/Netbook Laptop/Netbook         Tablet         Tablet

或者

library(dplyr)
library(tidyr)
df2 %>% 
    group_by(B0022HOME_NO) %>%
    mutate(N = paste0('Device_Detail', row_number())) %>%
    spread(N, Device_Detail, fill='')
# B0022HOME_NO Device_Detail1 Device_Detail2 Device_Detail3 Device_Detail4
#         (int)          (chr)          (chr)          (chr)          (chr)
#1      1043036 Laptop/Netbook         Tablet                              
#2      3753259 Laptop/Netbook Laptop/Netbook         Tablet         Tablet