Rowwise median for multiple columns using dplyr

后端 未结 1 1273
难免孤独
难免孤独 2021-01-07 13:40

Given the following dataset, I want to compute for each row the median of the columns M1,M2 and M3. I am looking for a solution where the final column is added to the datafr

1条回答
  •  生来不讨喜
    2021-01-07 14:10

    We could use rowMedians

    library(matrixStats)
    library(dplyr)
    df %>% 
        mutate(Median = rowMedians(as.matrix(.[grep('M\\d+', names(.))])))
    

    Or if we need to use only tidyverse functions, convert it to 'long' format with gather, summarize by row and get the median of the 'value' column

    df %>% 
        rownames_to_column('rn') %>%
        gather(key, value, starts_with('M')) %>%
        group_by(rn) %>% 
        summarise(Median = median(value)) %>%
        ungroup %>% 
        select(-rn) %>%
        bind_cols(df, .)
    

    Or another option is rowwise() from dplyr (hope the row is not a problem)

    df %>% 
       rowwise() %>% 
       mutate(Median =  median(c(!!! rlang::syms(grep('M', names(.), value=TRUE)))))
    

    0 讨论(0)
提交回复
热议问题