Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum variables within R dataframe

Tags:

r

I'm sorry, I'm sure a similar question has already been asked but I'm afraid I can't find it. I merely want to sum a lot of variables within a dataframe. As a small example, what I'd like to do is calculate df$e as below.

df <- data.frame(a=c(1,2,3,4,5), b=(c(6,7,8,9,10)), 
                 c=c(1,2,3,4,5), d=(c(6,7,8,9,10)))

df$e <- with(df, a+b+c+d) # this is the right answer

But I want to express it by saying df$e <- "the sum of all the variables between a and d."

Thank you! Help also appreciated in tagging.

ANSWER: df$e <- rowSums(subset(df, select=a:d))

I didn't express that I needed it very clearly, but I was as ignorant of subset as I was of rowSums.

like image 827
Grateful Guy Avatar asked Mar 10 '12 23:03

Grateful Guy


2 Answers

Are you looking for rowSums() ?

> df <- data.frame(a=c(1,2,3,4,5), b=(c(6,7,8,9,10)), 
+                  c=c(1,2,3,4,5), d=(c(6,7,8,9,10)))
> with(df, a+b+c+d)
[1] 14 18 22 26 30
> rowSums(df)
[1] 14 18 22 26 30
> 

which you can of course also assign back into df:

> df$e <- rowSums(df)
> df
  a  b c  d  e
1 1  6 1  6 14
2 2  7 2  7 18
3 3  8 3  8 22
4 4  9 4  9 26
5 5 10 5 10 30
> 
like image 58
Dirk Eddelbuettel Avatar answered Oct 02 '22 07:10

Dirk Eddelbuettel


Are you looking for a way to not explicitly write out a + b + c + d + ...?

If so, how about rowSums()

df$e <- with(df, rowSums(df))
like image 44
Chase Avatar answered Oct 02 '22 08:10

Chase