Grateful Guy
Grateful Guy

Reputation: 137

sum variables within R dataframe

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.

Upvotes: 8

Views: 33730

Answers (2)

Chase
Chase

Reputation: 69201

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))

Upvotes: 3

Dirk is no longer here
Dirk is no longer here

Reputation: 368271

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
> 

Upvotes: 11

Related Questions