mutate rowSums exclude one column

前端 未结 3 452
梦毁少年i
梦毁少年i 2021-02-08 10:55

I have a data frame like this

> df
Source: local data frame [4 x 4]

      a x y z
1 name1 1 1 1
2 name2 1 1 1
3 name3 1 1 1
4 name4 1 1 1

W

3条回答
  •  终归单人心
    2021-02-08 11:28

    This should work:

    #dummy data    
    df <- read.table(text="a x y z
    
    1 name1 1 1 1
    2 name2 1 1 1
    3 name3 1 1 1
    4 name4 1 1 1",header=TRUE)
    
    library(dplyr)
    
    df %>% select(-a) %>% mutate(total=rowSums(.)) 
    

    First exclude text column - a, then do the rowSums over remaining numeric columns.

提交回复
热议问题