I want to sum specific rows by my choice. my data is like this :
df_new <- data.frame(country = c('US', 'US','US', 'UK','UK','UK'),
team = c('A', 'B','C','A','F', 'E'),
x1990 = c(11, 8,2,16,1,21),
x2005 = c(6, 4,2,10,1, 14))
and I want to prepare it like this:
df_sum <- data.frame(country = c('US','US', 'UK','UK'),
team = c('A', 'B','A','F'),
x1990 = c(11, 10,16,22),
x2005 = c(6, 6,10, 15))
CodePudding user response:
You may try
df_new%>%
mutate(team = case_when(
team %in% c("B", "C") ~ "B",
team %in% c("E", "F") ~ "F",
T ~ team
)) %>%
group_by(country, team) %>%
summarise(across(is.numeric, ~sum(.x))) %>%
arrange(desc(country))
country team x1990 x2005
<chr> <chr> <dbl> <dbl>
1 US A 11 6
2 US B 10 6
3 UK A 16 10
4 UK F 22 15
You may need to specify your some inside case_when
part.