How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

前端 未结 8 690
無奈伤痛
無奈伤痛 2020-12-04 18:34

I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame.

8条回答
  •  眼角桃花
    2020-12-04 19:18

    For a hackish tidy solution, check this out:

    library(tidyr)
    library(dplyr)
    
    df %>% 
       rownames_to_column(var = 'row') %>% 
       gather(a1:c2, key = 'key', value = 'value') %>% 
       extract(key, into = c('col.base', 'col.index'), regex = '([a-zA-Z]+)([0-9]+)') %>% 
       group_by(row, col.base) %>% 
       summarize(.sum = sum(value)) %>%
       spread(col.base, .sum) %>% 
       bind_cols(df, .) %>% 
       select(-row)
    

    Basically, I collect all pairs of columns with their values across all rows, separate the column name in two parts, calculate the row sums for columns with the same letter, and cast it back to the wide form.

提交回复
热议问题