Applying group_by and summarise(sum) but keep columns with non-relevant conflicting data?

后端 未结 3 1603
生来不讨喜
生来不讨喜 2021-01-11 23:17

My question is very similar to Applying group_by and summarise on data while keeping all the columns' info but I would like to keep columns which get excluded because th

3条回答
  •  感情败类
    2021-01-11 23:56

    Here are two other options:

    1) Nest columns into one column and then customize the summary by checking the data types:

    df %>% 
        group_by(Label, Code) %>% nest() %>% 
        mutate(data = map(data, 
            ~ as.tibble(map(.x, ~ if(is.numeric(.x)) sum(.x) else paste(.x, collapse="_")))
              )
        ) %>% unnest()
    
    # A tibble: 6 x 6
    #   Label   Code                        Type Proportion     N     C
    #                                  
    #1   203c      c                   wholefish    1.00000     1     1
    #2   203c      a                       flesh    1.00000     2     2
    #3   204a      a               flesh_formula    0.99999     8     8
    #4   204a      b     fleshdelip_formuladelip    0.99999    10    10
    #5   204a      c           formula_wholefish    0.99999    16    16
    #6   204a      d formuladelip_wholefishdelip    0.99999    18    18
    

    2) summarize separately and then join the result:

    numeric <- df %>% 
        group_by(Label, Code) %>% 
        summarise_if(is.numeric, sum)
    
    character <- df %>% 
        group_by(Label, Code) %>% 
        summarise_if(~ is.character(.) || is.factor(.), ~ paste(., collapse="_"))
    
    inner_join(numeric, character, by = c("Label", "Code"))
    # A tibble: 6 x 6
    # Groups:   Label [?]
    #   Label   Code Proportion     N     C                        Type
    #                                  
    #1   203c      a    1.00000     2     2                       flesh
    #2   203c      c    1.00000     1     1                   wholefish
    #3   204a      a    0.99999     8     8               flesh_formula
    #4   204a      b    0.99999    10    10     fleshdelip_formuladelip
    #5   204a      c    0.99999    16    16           formula_wholefish
    #6   204a      d    0.99999    18    18 formuladelip_wholefishdelip
    

提交回复
热议问题