使用group_by组合R中的多个字符行并汇总

时间:2019-02-13 07:27:16

标签: r summarize

我有一个类似于以下内容的数据集

Invoice Pizza Pasta Soda Cake  
 1        NA  pasta  NA   NA    
 1        NA   NA    NA  cake    
 2      pizza  NA    NA   NA    
 2        NA  pasta  NA   NA

我想按发票分组并得到

下的输出
Invoice Pizza Pasta Soda Cake  
 1        NA  pasta  NA  cake   
 2     pizza  pasta  NA   NA

我正在尝试使用dplyr的group_by(Invoice) %>% summarize()功能,但无法获得所需的输出。请提出一个好的方法,谢谢!

1 个答案:

答案 0 :(得分:1)

library(dplyr)
df %>% group_by(Invoice) %>% 
       summarise_all(funs(sub('NA,|,NA','',paste(.,collapse = ','))))

# A tibble: 2 x 5
  Invoice Pizza Pasta Soda  Cake 
    <int> <chr> <chr> <chr> <chr>
1       1 NA    pasta NA    cake 
2       2 pizza pasta NA    NA