Reputation: 117
I have this dataframe (there are 20 more columns like class
but just for the example I show a short version):
niv ID class1 class2 class3
A x 10 5 7
A y 15 3 1
A z 11 2 4
B u 17 7 3
B w 18 7 9
So I wanted to group_by(niv)
and to summarise the ID
column just counting it and class1
, class2
and class3
columns to add up in just one command. I tried summarise_each
but I can't sort out how to use it. Also the objetive would be to learn how to mix different funs in the summarise function when you have a dataframe with a lot of columns.
The summarise dataframe I need is this one:
niv n_ID sum_class1 sum_class2 sum_class3
A 3 36 10 12
B 2 35 14 12
Thanks in advance.
Upvotes: 1
Views: 66
Reputation: 886938
We can use summarise
with across
library(dplyr)
df %>%
mutate(n_ID = 1) %>%
group_by(niv) %>%
summarise(across(starts_with('class'), sum))
With across
, we can have multiple functions
iris %>%
summarise(across(where(is.factor), nlevels),
across(where(is.numeric), mean))
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#1 3 5.843333 3.057333 3.758 1.199333
df <- structure(list(niv = c("A", "A", "A", "B", "B"), ID = c("x",
"y", "z", "u", "w"), class1 = c(10L, 15L, 11L, 17L, 18L), class2 = c(5L,
3L, 2L, 7L, 7L), class3 = c(7L, 1L, 4L, 3L, 9L)),
class = "data.frame", row.names = c(NA, -5L))
Upvotes: 1
Reputation: 388807
You can create n_ID
column with all 1's and sum
them with all 'class'
columns.
library(dplyr)
df %>%
mutate(n_ID = 1) %>%
group_by(niv) %>%
summarise_at(vars(n_ID, starts_with('class')), sum)
# A tibble: 2 x 5
# niv n_ID class1 class2 class3
# <chr> <dbl> <int> <int> <int>
#1 A 3 36 10 12
#2 B 2 35 14 12
data
df <- structure(list(niv = c("A", "A", "A", "B", "B"), ID = c("x",
"y", "z", "u", "w"), class1 = c(10L, 15L, 11L, 17L, 18L), class2 = c(5L,
3L, 2L, 7L, 7L), class3 = c(7L, 1L, 4L, 3L, 9L)),
class = "data.frame", row.names = c(NA, -5L))
Upvotes: 0