Search code examples
rdataframedplyrmultiple-columns

Extract separate columns from data frame


I am trying to extract the first names of the titles of the columns such as pack_10, pack_18 and pack_20 and group all of them with sum. Below you can see my data

df<-data.frame(  
              packs_10_value5=c(100,0,0,0,0),
              packs_18_value9=c(200,0,0,0,0),
              packs_20_value13=c(300,0,0,0,0),
              packs_10_value15=c(100,0,0,0,0),
              packs_18_value17=c(200,0,0,0,0),
              packs_20_value18=c(300,0,0,0,0)
              )

df

enter image description here

So can anybody help me with how to solve this?


Solution

  • You can split the columns and apply rowSums by group:

    library(purrr)
    split.default(df, f = gsub("_value.*", "", names(df))) %>% 
      map_dfc(rowSums)
    
    # A tibble: 5 × 3
      packs_10 packs_18 packs_20
         <dbl>    <dbl>    <dbl>
    1      200      400      600
    2        0        0        0
    3        0        0        0
    4        0        0        0
    5        0        0        0