I have this df:
structure(list(CN = c("BR", "BR", "BR", "PL", "PL", "PL",
"BR", "BR", "BR", "BR", "PL", "PL", "PL"), Year = c(2019,
2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020,
2020), Squad = c("A", "B", "C", "A", "B", "C", "C", "F", "G",
"I", "D", "E", "F"), X = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3,
1), Y = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1)), row.names = c(NA,
-13L), class = c("tbl_df", "tbl", "data.frame"))
I want to summarize (sum of x y and squad count) grouped by CN and Year; and in the same structure add a column with the count of unique/distinct values for squad grouped by CN only.
It would look like this:
structure(list(CN = c("BR", "BR", "PL", "PL"), Year = c(2019,
2020, 2019, 2020), Sum = c(12, 14, 12, 12), n_squad = c(3, 4,
3, 3), n_squad_distinct = c(6, 6, 6, 6)), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))
Thanks
CodePudding user response:
We may create the 'n_squad_distinct' column grouped by 'CN" by applying n_distinct
on the 'Squad', then add the 'Year' and 'n_squad_distinct' also as grouping variables and do the summarise
library(dplyr)
df %>%
group_by(CN) %>%
mutate(n_squad_distinct = n_distinct(Squad)) %>%
group_by(n_squad_distinct, Year, .add = TRUE) %>%
summarise(Sum = sum(X Y), n_squad = n_distinct(Squad), .groups = 'drop')
-output
# A tibble: 4 × 5
CN n_squad_distinct Year Sum n_squad
<chr> <int> <dbl> <dbl> <int>
1 BR 6 2019 12 3
2 BR 6 2020 14 4
3 PL 6 2019 12 3
4 PL 6 2020 12 3