合并具有相同名称的行,并对其他列行中的其他值求和

  • 本文关键字:其他 求和 合并 r
  • 更新时间 :
  • 英文 :

>ay">Jun>2742>>td style="text align=center;">44761>589>td style="text-align:centre!">40>>td style="text-align:center;">96>td style="text-align:right;">898>td style="text align:central;">120>2<1d>28<24><1051>捷克共和国center;">36027333
2000年 Jan FebMarApr7月8月9月
欧洲 513345666140022340853897734730
白俄罗斯 108815550765374108
保加利亚 2031108247587475188
377536596

我们可以首先使用group_bycountry

然后使用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"))

您可以使用baseRaggregate()或dplyrgroup_by(),然后使用summarise()

最新更新