首页 > 解决方案 > 合并具有相同名称的行并对其他列行中的其他值求和

问题描述

2000 二月 三月 四月 可能 七月 八月 九月 十月 十一月 十二月 全部的
欧洲 51,334 52,742 56,661 40,022 34,085 23,549 30,658 38,977 34,730 44,761 47,895 50,007 505,421
白俄罗斯 108 58 81 99 40 55 50 76 53 74 96 108 898
保加利亚 203 120 110 82 32 28 34 47 58 74 75 188 1,051
捷克共和国 489 640 570 342 236 236 185 360 273 377 536 533 596
2010 二月 三月 四月 可能 七月 八月 九月 十月 十一月 十二月 全部的
欧洲 51,334 52,742 56,661 40,022 34,085 23,549 30,658 38,977 34,730 44,761 47,895 50,007 505,421
白俄罗斯 108 58 81 99 40 55 50 76 53 74 96 108 898
保加利亚 203 120 110 82 32 28 34 47 58 74 75 188 1,051
捷克共和国 489 640 570 342 236 236 185 360 273 377 536 533 596

标签: r

解决方案


我们可以先group_by country

然后summariseacross

library(dplyr)
df %>% 
  group_by(country) %>% 
  summarise(across(everything(), sum))

输出:

 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

数据负责人:

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"))

推荐阅读