将多行合并为单行

时间:2019-10-01 20:36:23

标签: r

i每个“ customerID”都有多个条目,并且每个条目在“编号”,“类型”,“时间”列中都有一个订单号,订单类型和订单时间。我想根据订单号“ Number”将多行合并为一行。对于每个客户,订购时间和订单号的顺序是递增的。

xxx <- data.frame(CustomerID = c(1,1,1,2,2,3,4,4,4,4,5), Number = c(1,2,3,1,2,1,1,2,3,4,1), type = c("ball", "pen", "compass", "ball", "eraser", "ball", "watch", "pen", "ball", "scale", "ball"), time = c(0.58, 1.23, 2.34, 2.11, 4.57, 0.58, 1.02, 3.23, 4.32, 5.45, 0.23) )

预期输出为

customerID  Number_1   Number_2 Number_3 Number_4   type_1   Type_2   type_3                                                                      
1                1           2         3       NA     ball     pen    compass
2                 1          2         NA      NA     ball     eraser  NA
3                1          NA         NA      NA     ball     NA      NA
time_1   time_2 time_3 time_4
```0.58    1.23    2.34    2.11
2.11       4.57     NA     NA
0.58       NA        NA    NA\

1 个答案:

答案 0 :(得分:3)

我们可以使用pivot_wider中的tidyr

library(dplyr)
library(tidyr)
xxx %>%
   pivot_wider(values_from = c(Number, type, time), names_from =  Number)
# A tibble: 5 x 13
#  CustomerID Number_1 Number_2 Number_3 Number_4 type_1 type_2 type_3  type_4 time_1 time_2 time_3 time_4
#       <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <fct>  <fct>  <fct>   <fct>   <dbl>  <dbl>  <dbl>  <dbl>
#1          1        1        2        3       NA ball   pen    compass <NA>    0.580   1.23   2.34  NA   
#2          2        1        2       NA       NA ball   eraser <NA>    <NA>    2.11    4.57  NA     NA   
#3          3        1       NA       NA       NA ball   <NA>   <NA>    <NA>    0.580  NA     NA     NA   
#4          4        1        2        3        4 watch  pen    ball    scale   1.02    3.23   4.32   5.45
#5          5        1       NA       NA       NA ball   <NA>   <NA>    <NA>    0.23   NA     NA     NA   

如果“ CustomerID”和“ Number”重复,则创建一个按这些列分组的序列列,然后执行pivot_wider

xxx %>% 
  group_by(CustomerID, Number) %>% 
  mutate(rn = row_number()) %>%
  pivot_wider(values_from = c(Number, type, time), names_from =  Number)