add column with row wise mean over selected columns using dplyr

前端 未结 3 725
心在旅途
心在旅途 2020-12-21 00:49

I have a data frame which contains several variables which got measured at different time points (e.g., test1_tp1, test1_tp2, test1_tp3

相关标签:
3条回答
  • 2020-12-21 01:02

    Not a dplyr solution, but you can try:

    cols_2sum <- grepl('test1',colnames(data))
    rowMeans(data[,cols_2sum])
    
    0 讨论(0)
  • 2020-12-21 01:05

    You can use starts_with inside select to find all columns starting with a certain string.

    data %>%
      mutate(test1 = select(., starts_with("test1_")) %>%
               rowMeans(na.rm = TRUE))
    
    0 讨论(0)
  • 2020-12-21 01:22

    Here's how you could do it in dplyr - I use the iris data as an example:

    iris %>% mutate(sum.Sepal = rowSums(.[grep("^Sepal", names(.))]))
    

    This computes rowwise sums of all columns that start with "Sepal". You can use rowMeans instead of rowSums the same way.

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