Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mutate rowSums exclude one column

Tags:

r

dplyr

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

Want to mutate it by adding columns x, y, and z (there can be many more numeric columns). Trying to exclude column 'a' as follows is not working.

dft <- df %>% mutate(funs(total = rowSums(.)), -a)
Error: not compatible with STRSXP

This also produces an error:

dft <- df %>% mutate(total = rowSums(.), -a)
Error in rowSums(.) : 'x' must be numeric

What is the right way?

like image 811
Gopala Avatar asked May 06 '15 18:05

Gopala


1 Answers

You can use rich selectors with select() inside the call to rowSums()

df %>% transmute(a, total = rowSums(select(., -a)))
like image 156
jenswirf Avatar answered Oct 25 '22 23:10

jenswirf