Search code examples
rrows

Combine rows into one in R


I have a dataframe which looks like this:

ID  Name                m1  m2  m3  m4  m5  m6  m7  m8
37  Grötlingbo Roes II  0   0   0   0   1   0   0   0   
37  Grötlingbo Roes II  0   1   0   0   0   0   0   0   
37  Grötlingbo Roes II  0   0   1   0   0   0   0   0   
123 Hablingbo Havor III 0   0   0   0   0   0   0   0   
123 Hablingbo Havor III 0   0   0   0   0   0   1   0   
38  Hablingbo Havor I   0   0   1   0   0   0   0   0   
38  Hablingbo Havor I   0   0   0   0   0   0   0   0   
38  Hablingbo Havor I   0   0   0   0   0   0   0   0   
38  Hablingbo Havor I   0   0   0   1   0   0   0   0   
38  Hablingbo Havor I   0   1   0   0   0   1   0   0

I need to combine rows with the same name and their observations in columns m1 to m8 into one row. Only the row names are identical but not the observations. I know that the observations will not collide because I was already tidying the dataframe before. Since I don't want to add new columns, I cannot use pivot_wider. And I believe the unite function is only for uniting columns, not rows. I feel like it could be done with group_by but don't know what I need to add. I was already reading many entries here but did not find an answer to my problem yet. Does anyone have a solution?


Solution

  • Try the summarize(across()) approach:

    library(dplyr)
    df %>%
      group_by(ID, Name) %>%
      summarize(across(starts_with("M"), sum))
    
    `summarise()` regrouping output by 'ID' (override with `.groups` argument)
    # A tibble: 3 x 10
    # Groups:   ID [3]
         ID Name                       m1    m2    m3    m4    m5    m6    m7    m8
      <dbl> <chr>                   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1    37 "Grötlingbo Roes II"        0     1     1     0     1     0     0     0
    2    38 "Hablingbo Havor I"         0     1     1     1     0     1     0     0
    3   123 "Hablingbo Havor III"       0     0     0     0     0     0     1     0