dplyr - using mutate() like rowmeans()

前端 未结 6 1678
后悔当初
后悔当初 2020-12-01 07:55

I can\'t find the answer anywhere.

I would like to calculate new variable of data frame which is based on mean of rows.

For example:

data &l         


        
6条回答
  •  借酒劲吻你
    2020-12-01 08:21

    I think the answer suggesting using data.frame or slicing on . is the best, but could be made simpler and more dplyr-ish like so:

    data %>% mutate(c = rowMeans(select(., a,b)))
    

    Or if you want to avoid ., with the penalty of having two inputs to your pipeline:

    data %>% mutate(c = rowMeans(select(data, a,b)))
    

提交回复
热议问题