We could first group_by
country
and then use summarise
with across
library(dplyr)
df %>%
group_by(country) %>%
summarise(across(everything(), sum))
Output:
country new_persons_vac~ total_persons_v~ new_persons_ful~ total_persons_f~ new_vaccine_dos~ total_vaccine_d~
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Afghan~ 294056 8452317 163535 2313338 457591 10765655
2 Albania 601152 27639676 465433 18105836 459226 45745512
3 Andorra 40569 360995 25838 144358 58535 506402
4 Angola 371996 9545624 559633 4688357 931629 14233981
5 Anguil~ 3206 73046 6847 48524 10053 121570
6 Antigu~ 5232 770379 26084 485839 31316 1256218
7 Argent~ 65820302 3858592405 16136889 917220373 81957191 4775812778
8 Armenia 138306 426851 58214 135848 196520 562699
9 Aruba 55435 4907836 52549 3439184 107984 8347020
10 Austra~ 14227655 811027845 5722445 163311327 19238830 974339172
# ... with 183 more rows
head of data:
df <- structure(list(country = c("Brazil", "Brazil", "Brazil", "Brazil",
"Brazil", "Brazil"), new_persons_vaccinated = c(1, 1, 1, 1, 1,
1), total_persons_vaccinated = c(1, 1, 1, 2, 1, 1), new_persons_fully_vaccinated = c(0,
0, 0, 0, 0, 0), total_persons_fully_vaccinated = c(0, 0, 0, 0,
0, 0), new_vaccine_doses_administered = c(1, 1, 1, 1, 1, 1),
total_vaccine_doses_administered = c(1, 1, 1, 2, 1, 1)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
7
solved Merge rows with same names and sum other values in other column rows [duplicate]