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.
Are you looking for
rowSums()?which you can of course also assign back into
df: