0

I have the following data frame data

    ID A  B  C
    X  1  1  1
    X  0  1  0
    Y  2  0  0
    Z  0  0  0

And I want to use group_by and summarise (dplyr package) to create a new data frame with sum of all values of A and B and C, group by ID.

Output should be like below:

    ID A  B  C
    X  1  2  1
    Y  2  0  0
    Z  0  0  0

My code is below:

    data_new <- data %>%
group_by(ID) %>%
summarise(Total.A = sum(A)) %>%
summarise(Total.B = sum(B))

However, the code only works with summing A and after that, I have an error says

   Error in summarise_impl(.data, dots) : Evaluation error: object 'B' not found.

Could you please help out?

lbrrrr
  • 65
  • 1
  • 11

2 Answers2

1

In dplyr summarise_all() applies the functions to all (non-grouping) columns as shown below:

--- Data Frame Creation---

ID <- c("X","X","Y","Z")

A <- c(1,0,2,0)

B <- c(1,1,0,0) 

C <- c(1,0,0,0)

df <- data.frame(ID,A,B,C)    

--- Simple Usage---

df %>% 

  group_by(ID) %>% 

  summarise_all(sum)
aosmith
  • 34,856
  • 9
  • 84
  • 118
Rahul Pant
  • 707
  • 5
  • 7
0

Put >1 expression in the summarise:

d %>% group_by(ID) %>% summarise(Total.A=sum(A), Total.B=sum(B))
# A tibble: 3 x 3
      ID Total.A Total.B
  <fctr>   <int>   <int>
1      X       1       2
2      Y       2       0
3      Z       0       0

and use summarise_all if you want to summarise all the things.

> d %>% group_by(ID) %>% summarise_all(sum)
# A tibble: 3 x 4
      ID     A     B     C
  <fctr> <int> <int> <int>
1      X     1     2     1
2      Y     2     0     0
3      Z     0     0     0
Spacedman
  • 92,590
  • 12
  • 140
  • 224