1

Got a df:

ID   Val1    Val2    Val3  
A    1        1       1
A    1        1       1
A    1        1       1
B    0        0       1

I want to take the sum of all the columns, based on a unique ID value. Like this:

ID   Val1    Val2    Val3     
A     3       3       3
B     0       0       1

I tried:

df %>% group_by(ID) %>% summarise_all(funs(sum()))

Anyone have an advice about what I' m doing wrong? I prefer a dplyr approach (if possible).

3
  • 3
    dat %>% group_by(ID) %>% summarise_all(sum) ? Commented Jan 16, 2018 at 13:29
  • 1
    The question is not merely a typographical issue, and highlights a potentially confusing aspect of the R language, in that functions can be invoked both with and without parentheses. In many cases, this difference has no effect (both count and count() will work as part of a dplyr chain). But in certain cases, the presence of parenthesis makes the difference between "call the function" versus "pass the function as an argument", as explained in the answer below. Commented Jan 16, 2018 at 15:36
  • Why load a package? aggregate(. ~ ID, df, sum) does it. Find simpler. Commented Jan 16, 2018 at 15:40

2 Answers 2

3

You need to remove the parentheses after sum, i.e., your code should read:

df %>% group_by(ID) %>% summarise_all(funs(sum))

Typing sum() in this case calls the function, whereas passing just the name of the function sends it to be used by summarise_all. It's the difference between saying "use this function here and now," versus, "pass the function, as a parameter, to some other function". Similarly, typing, ?sum will bring you the documentation for the function, but ?sum() is invalid.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I was so close. This is the answer that I was looking for! TY.
0

Edited*:

I don't know a solution using dplyr, but I do another solution using plyr, if interested.

You have:

   df=data.frame(id=c("A","A","A","B"), Val1=c(1,1,1,0), Val2=c(1,1,1,0),Val3=c(1,1,1,1))

> df
  id Val1 Val2 Val3
1  A    1    1    1
2  A    1    1    1
3  A    1    1    1
4  B    0    0    1

Using the plyr libray

> library(plyr)

> ddply(df,.(id),summarize,Val1=sum(Val1),Val2=sum(Val2),Val3=sum(Val3))

Output:

  id Val1 Val2 Val3
1  A    3    3    3
2  B    0    0    1

1 Comment

No problem, it's generally fine and accepted to post answers using different methods. I just found the wording a little surprising. I'll delete my comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.