简体   繁体   中英

R: Group data frame by attaching columns

I want to group rows of my data frame. But the columns should not be summarized, I want to attach them.

I tried the tidyverse package and came to a close (wrong) solution.

This is my df:

  `Order ID` Quantity `Sub-Category 1` `Sub-Category 2` `Sub-Category 3` `Sub-Category 4` `Sub-Category 5` `Sub-Category 6`
  <chr>         <dbl> <chr>            <chr>            <chr>            <chr>            <chr>            <chr>           
1   22              2 Bookcases        Bookcases        NA               NA               NA               NA              
2   33              3 Chairs           Chairs           Chairs           NA               NA               NA              
3   22              2 Labels           Labels           NA               NA               NA               NA              
4   33              5 Tables           Tables           Tables           Tables           Tables           NA              

The number of columns represent the Quantity, so Quantity column not longer needed. I want to group them and attach the same Order ID rows, to get this solution:

  `Order ID` Quantity `Sub-Category 1` `Sub-Category 2` `Sub-Category 3` `Sub-Category 4` `Sub-Category 5` `Sub-Category 6`
  <chr>         <dbl> <chr>            <chr>            <chr>            <chr>            <chr>            <chr>           
1   22              2 Bookcases        Bookcases        Labels               Labels               NA               NA              
2   33              3 Chairs           Chairs           Chairs           Tables               Tables               Tables               Tables               Tables

Thank you very much, appreciate your help.

Using the tidyr package's spread and gather functions, maybe try:

library(tidyverse)


df <- data %>% 
  select(-Quantity) %>%
  gather(key = "col", value = "val", -`Order ID`) %>% 
  group_by(`Order ID`, col) %>% 
  mutate(val = max(val, na.rm = T)) %>% 
  slice(1) %>% 
  ungroup() %>% 
  spread(key = "col", value = "val")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM