需要帮助弄清楚-R函数

时间:2020-07-01 19:07:32

标签: r

我有一个包含订单信息的客户表(表1),我试图找出一种方法是否可以使用R像下面的表2一样进行复制?

表1

Customer Name gender order        date
1        Tony  M     Pizza        12/20/2019
1        Tony  M     onion rings  12/29/2019
1        Tony  M     Fries        12/30/2019
2        Anna  F     Pizza        1/3/2020
3        Mike  M     Pizza        12/3/2019
3        Mike  M     Fries        1/14/2020

表2

customer  repeated_instrument  repeated_instance name  gender  order   date 
1                                                Tony  M                                              
1         order                1                               Pizza   12/20/2019  
1         order                2                               oni_rng 12/29/2020
1         order                3                               Fries   12/30/2019
2                                                Anna  F
2         order                1                               Pizza   1/2/2020
3                                                Mike  M
3         order                1                               Pizza   12/3/2019
3         order                2                               Fries   1/4/2020

Table 1

Table 2

1 个答案:

答案 0 :(得分:0)

您可以使用dplyr

library(dplyr)

df %>% 
  group_by(Name) %>% 
  mutate(repeated_instrument = "order",
         repeated_instance = row_number(Customer)) %>% 
  do(rbind(.,c(.$Customer,rep(NA,ncol(df)-1)))) %>% 
  arrange(Customer, repeated_instance) %>% 
  group_by(Customer) %>% 
  mutate(name = ifelse(is.na(order), first(Name), NA),
         gender = ifelse(is.na(order), first(gender), NA)) %>% 
  ungroup() %>% 
  mutate(repeated_instance = ifelse(is.na(order), NA, repeated_instance)) %>% 
  select(Customer, repeated_instrument, repeated_instance, name, gender, order, date) %>% 
  arrange(Customer, name, repeated_instance)

哪个给我们:

  Customer repeated_instrument repeated_instance name  gender order       date      
     <dbl> <chr>                           <dbl> <chr> <chr>  <chr>       <chr>     
1        1 NA                                 NA Tony  M      NA          NA        
2        1 order                               1 NA    NA     Pizza       12/20/2019
3        1 order                               2 NA    NA     onion rings 12/29/2019
4        1 order                               3 NA    NA     Fries       12/30/2019
5        2 2                                  NA Anna  F      NA          NA        
6        2 order                               1 NA    NA     Pizza       1/3/2020  
7        3 NA                                 NA Mike  M      NA          NA        
8        3 order                               1 NA    NA     Pizza       12/3/2019 
9        3 order                               2 NA    NA     Fries       1/14/2020 

数据:

structure(list(Customer = c(1, 1, 1, 2, 3, 3), Name = c("Tony", 
"Tony", "Tony", "Anna", "Mike", "Mike"), gender = c("M", "M", 
"M", "F", "M", "M"), order = c("Pizza", "onion rings", "Fries", 
"Pizza", "Pizza", "Fries"), date = c("12/20/2019", "12/29/2019", 
"12/30/2019", "1/3/2020", "12/3/2019", "1/14/2020")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))