Adding A Sets Of Rows Without Count

How do I add repeated rows together (not as a total; only certain rows)? For instance, Asia (repeated 17 times, would be it' own sum), then UK (repeated 5, would be it's own sum and new column). There are over 2000. So how can I add specific rows together without having to name/number each of them?

** Asia add**
UK

1. Asia 2876
2. Asia 1873
3. Asia 3214
4. Asia 2412
5. UK 8773
6. UK 3552
7. UK 3525
8.UK 2542
9. UK 2342
10. UK 9814

Note: There are 70 countries, over 2000 rows.

Hi @amerceda, welcome to RStudio Community.

Is this what you are trying to do?

library(tidyverse)
#> Warning: package 'forcats' was built under R version 3.6.3

df <- tribble(~Country, ~Value, 
              "Asia", 2876,
              "Asia", 1873,
              "Asia", 3214,
              "Asia", 2412,
              "UK", 8773,
              "UK", 3552,
              "UK", 3525,
              "UK", 2542,
              "UK", 2342,
              "UK", 9814)

df %>% 
  group_by(Country) %>% 
  summarize(Total_Value = sum(Value))
#> # A tibble: 2 x 2
#>   Country Total_Value
#>   <chr>         <dbl>
#> 1 Asia          10375
#> 2 UK            30548

Created on 2020-03-06 by the reprex package (v0.3.0)

Is there any way I can do it without typing down all 2000 rows?

Its likely you have the 2000 rows of data in some excel or csv file?
You can import the same via readlx::read_excel or readr::read_csv and then use the code provided earlier to get the summary.

df<- readxl::read_excel("enter path to excel file")
OR
df<- readr::read_csv("enter path to csv file")

@siddharthprabhu code:
df %>%
group_by(Country) %>%
summarize(Total_Value = sum(Value))

1 Like

The second part of the answer from @siddharthprabhu should work with your dataset. Just replace the dataset and column names with whatever you are using.

The problem is, there are 70 countries in total. Is there really no simpler way?

I don't think you understand what the code is doing. It is summarising all the counts by country. This will work whether there are two countries or 200 countries.

If the example wasn't sufficient, please provide your own reproducible example:

1 Like

So this iswhat I typed down:

covid19_countries <- covid19 %>%   
  df %>%
  group_by(country, cases, deaths, recovered) %>%
  summarize(Total_Value = sum(Value))

But I got this error:
Error in df(.) : argument "df1" is missing, with no default

How do I fix it?

What does covid19 look like?
What do you want to summarise? Deaths and Recovered?

Edited below (again). Try this:

covid19_countries <- covid19 %>%   
  group_by(country) %>%
  summarise(total_deaths = sum(deaths), total_recovered = sum(recovered))

Sorry, edited a few times. It would be easier if you posted a reprex.

It looks like this:
covid19_daily.csv

country-obsv_date-days_country-cases-deaths-recovered

|1045|Thailand|2020-01-25|3|7|0|0|
|1046|Thailand|2020-01-26|4|8|0|2|
|1047|Thailand|2020-01-27|5|8|0|2|
|1048|Thailand|2020-01-28|6|14|0|5|
|1049|Thailand|2020-01-29|7|14|0|5|
|1050|Thailand|2020-01-30|8|14|0|5|
|1051|Thailand|2020-01-31|9|19|0|5|
|1052|Thailand|2020-02-01|10|19|0|5|
|1053|Thailand|2020-02-02|11|19|0|5|
|1054|Thailand|2020-02-03|12|19|0|5|
|1055|Thailand|2020-02-04|13|25|0|5|
|1056|Thailand|2020-02-05|14|25|0|5|
|1057|Thailand|2020-02-06|15|25|0|5|
|1058|Thailand|2020-02-07|16|25|0|5|
|1059|Thailand|2020-02-08|17|32|0|10|
|1060|Thailand|2020-02-09|18|32|0|10|
|1061|Thailand|2020-02-10|19|32|0|10|
|1062|Thailand|2020-02-11|20|33|0|10|
|1063|Thailand|2020-02-12|21|33|0|10|
|1064|Thailand|2020-02-13|22|33|0|12|
|1065|Thailand|2020-02-14|23|33|0|12|
|1066|Thailand|2020-02-15|24|33|0|12|
|1067|Thailand|2020-02-16|25|34|0|14|
|1068|Thailand|2020-02-17|26|35|0|15|
|1069|Thailand|2020-02-18|27|35|0|15|
|1070|Thailand|2020-02-19|28|35|0|15|
|1071|Thailand|2020-02-20|29|35|0|15|
|1072|Thailand|2020-02-21|30|35|0|17|
|1073|Thailand|2020-02-22|31|35|0|17|
|1074|Thailand|2020-02-23|32|35|0|21|
|1075|Thailand|2020-02-24|33|35|0|21|
|1076|Thailand|2020-02-25|34|37|0|22|
|1077|Thailand|2020-02-26|35|40|0|22|
|1078|Thailand|2020-02-27|36|40|0|22|
|1079|Thailand|2020-02-28|37|41|0|28|
|1080|Thailand|2020-02-29|38|42|0|28|
|1081|Thailand|2020-03-01|39|42|1|28|
|1082|UK|2020-01-31|0|2|0|0|
|1083|UK|2020-02-01|1|2|0|0|
|1084|UK|2020-02-02|2|2|0|0|
|1085|UK|2020-02-03|3|2|0|0|
|1086|UK|2020-02-04|4|2|0|0|
|1087|UK|2020-02-05|5|2|0|0|
|1088|UK|2020-02-06|6|2|0|0|
|1089|UK|2020-02-07|7|3|0|0|
|1090|UK|2020-02-08|8|3|0|0|
|1091|UK|2020-02-09|9|3|0|0|
|1092|UK|2020-02-10|10|8|0|0|
|1093|UK|2020-02-11|11|8|0|0|
|1094|UK|2020-02-12|12|9|0|1|
|1095|UK|2020-02-13|13|9|0|1|
|1096|UK|2020-02-14|14|9|0|1|
|1097|UK|2020-02-15|15|9|0|1|
|1098|UK|2020-02-16|16|9|0|8|
|1099|UK|2020-02-17|17|9|0|8|
|1100|UK|2020-02-18|18|9|0|8|
|1101|UK|2020-02-19|19|9|0|8|
|1102|UK|2020-02-20|20|9|0|8|
|1103|UK|2020-02-21|21|9|0|8|
|1104|UK|2020-02-22|22|9|0|8|
|1105|UK|2020-02-23|23|9|0|8|
|1106|UK|2020-02-24|24|13|0|8|
|1107|UK|2020-02-25|25|13|0|8|
|1108|UK|2020-02-26|26|13|0|8|
|1109|UK|2020-02-27|27|15|0|8|
|1110|UK|2020-02-28|28|20|0|8|
|1111|UK|2020-02-29|29|23|0|8|
|1112|UK|2020-03-01|30|36|0|8|
|1113|United Arab Emirates|2020-01-29|0|4|0|0|
|1114|United Arab Emirates|2020-01-30|1|4|0|0|
|1115|United Arab Emirates|2020-01-31|2|4|0|0|
|1116|United Arab Emirates|2020-02-01|3|4|0|0|
|1117|United Arab Emirates|2020-02-02|4|5|0|0|
|1118|United Arab Emirates|2020-02-03|5|5|0|0|
|1119|United Arab Emirates|2020-02-04|6|5|0|0|
|1120|United Arab Emirates|2020-02-05|7|5|0|0|
|1121|United Arab Emirates|2020-02-06|8|5|0|0|
|1122|United Arab Emirates|2020-02-07|9|5|0|0|
|1123|United Arab Emirates|2020-02-08|10|7|0|0|
|1124|United Arab Emirates|2020-02-09|11|7|0|0|
|1125|United Arab Emirates|2020-02-10|12|8|0|0|
|1126|United Arab Emirates|2020-02-11|13|8|0|0|
|1127|United Arab Emirates|2020-02-12|14|8|0|1|
|1128|United Arab Emirates|2020-02-13|15|8|0|1|
|1129|United Arab Emirates|2020-02-14|16|8|0|1|
|1130|United Arab Emirates|2020-02-15|17|8|0|3|
|1131|United Arab Emirates|2020-02-16|18|9|0|4|
|1132|United Arab Emirates|2020-02-17|19|9|0|4|
|1133|United Arab Emirates|2020-02-18|20|9|0|4|
|1134|United Arab Emirates|2020-02-19|21|9|0|4|
|1135|United Arab Emirates|2020-02-20|22|9|0|4|
|1136|United Arab Emirates|2020-02-21|23|9|0|4|
|1137|United Arab Emirates|2020-02-22|24|13|0|4|
|1138|United Arab Emirates|2020-02-23|25|13|0|4|
|1139|United Arab Emirates|2020-02-24|26|13|0|4|
|1140|United Arab Emirates|2020-02-25|27|13|0|4|
|1141|United Arab Emirates|2020-02-26|28|13|0|4|
|1142|United Arab Emirates|2020-02-27|29|13|0|4|
|1143|United Arab Emirates|2020-02-28|30|19|0|5|
|1144|United Arab Emirates|2020-02-29|31|21|0|5|
|1145|United Arab Emirates|2020-03-01|32|21|0|5|
|1146|US|2020-01-22|0|1|0|0|
|1147|US|2020-01-23|1|1|0|0|
|1148|US|2020-01-24|2|2|0|0|
|1149|US|2020-01-25|3|2|0|0|
|1150|US|2020-01-26|4|5|0|0|
|1151|US|2020-01-27|5|5|0|0|
|1152|US|2020-01-28|6|5|0|0|
|1153|US|2020-01-29|7|5|0|0|
|1154|US|2020-01-30|8|5|0|0|
|1155|US|2020-01-31|9|6|0|0|
|1156|US|2020-02-01|10|8|0|0|
|1157|US|2020-02-02|11|8|0|0|
|1158|US|2020-02-03|12|11|0|0|
|1159|US|2020-02-04|13|11|0|0|
|1160|US|2020-02-05|14|12|0|0|
|1161|US|2020-02-06|15|12|0|0|
|1162|US|2020-02-07|16|12|0|0|
|1163|US|2020-02-08|17|12|0|0|
|1164|US|2020-02-09|18|12|0|3|
|1165|US|2020-02-10|19|12|0|3|
|1166|US|2020-02-11|20|13|0|3|
|1167|US|2020-02-12|21|13|0|3|
|1168|US|2020-02-13|22|15|0|3|
|1169|US|2020-02-14|23|15|0|3|
|1170|US|2020-02-15|24|15|0|3|
|1171|US|2020-02-16|25|15|0|3|
|1172|US|2020-02-17|26|15|0|3|
|1173|US|2020-02-18|27|15|0|3|
|1174|US|2020-02-19|28|15|0|3|
|1175|US|2020-02-20|29|15|0|3|
|1176|US|2020-02-21|30|35|0|5|
|1177|US|2020-02-22|31|35|0|5|
|1178|US|2020-02-23|32|35|0|5|
|1179|US|2020-02-24|33|53|0|5|
|1180|US|2020-02-25|34|53|0|6|
|1181|US|2020-02-26|35|59|0|6|
|1182|US|2020-02-27|36|60|0|6|
|1183|US|2020-02-28|37|62|0|7|
|1184|US|2020-02-29|38|70|1|7|
|1185|US|2020-03-01|39|76|1|7|
|1186|Vietnam|2020-01-23|0|2|0|0|
|1187|Vietnam|2020-01-24|1|2|0|0|
|1188|Vietnam|2020-01-25|2|2|0|0|
|1189|Vietnam|2020-01-26|3|2|0|0|
|1190|Vietnam|2020-01-27|4|2|0|0|
|1191|Vietnam|2020-01-28|5|2|0|0|
|1192|Vietnam|2020-01-29|6|2|0|0|
|1193|Vietnam|2020-01-30|7|2|0|0|
|1194|Vietnam|2020-01-31|8|2|0|0|
|1195|Vietnam|2020-02-01|9|6|0|1|
|1196|Vietnam|2020-02-02|10|6|0|1|
|1197|Vietnam|2020-02-03|11|8|0|1|
|1198|Vietnam|2020-02-04|12|8|0|1|
|1199|Vietnam|2020-02-05|13|8|0|1|
|1200|Vietnam|2020-02-06|14|10|0|1|
|1201|Vietnam|2020-02-07|15|10|0|1|
|1202|Vietnam|2020-02-08|16|13|0|1|
|1203|Vietnam|2020-02-09|17|13|0|1|
|1204|Vietnam|2020-02-10|18|14|0|1|
|1205|Vietnam|2020-02-11|19|15|0|6|
|1206|Vietnam|2020-02-12|20|15|0|6|
|1207|Vietnam|2020-02-13|21|16|0|7|
|1208|Vietnam|2020-02-14|22|16|0|7|
|1209|Vietnam|2020-02-15|23|16|0|7|
|1210|Vietnam|2020-02-16|24|16|0|7|
|1211|Vietnam|2020-02-17|25|16|0|7|
|1212|Vietnam|2020-02-18|26|16|0|7|
|1213|Vietnam|2020-02-19|27|16|0|7|
|1214|Vietnam|2020-02-20|28|16|0|7|
|1215|Vietnam|2020-02-21|29|16|0|14|
|1216|Vietnam|2020-02-22|30|16|0|14|
|1217|Vietnam|2020-02-23|31|16|0|14|
|1218|Vietnam|2020-02-24|32|16|0|14|
|1219|Vietnam|2020-02-25|33|16|0|16|
|1220|Vietnam|2020-02-26|34|16|0|16|
|1221|Vietnam|2020-02-27|35|16|0|16|
|1222|Vietnam|2020-02-28|36|16|0|16|
|1223|Vietnam|2020-02-29|37|16|0|16|
|1224|Vietnam|2020-03-01|38|16|0|16|

Please read the reprex article. It will make it easier for others to help you. It will also help you with your code.

Assuming that you read the reprex article, you might have come up with something like this:

covid19 <- tibble::tribble(
                ~country,   ~obsv_date, ~days, ~cases, ~deaths, ~recovered,
              "Thailand", "25/01/2020",     3,      7,       0,          0,
              "Thailand", "26/01/2020",     4,      8,       0,          2,
              "Thailand", "27/01/2020",     5,      8,       0,          2,
              "Thailand", "28/01/2020",     6,     14,       0,          5,
              "Thailand", "29/01/2020",     7,     14,       0,          5,
              "Thailand", "30/01/2020",     8,     14,       0,          5,
              "Thailand", "31/01/2020",     9,     19,       0,          5,
              "Thailand",  "1/02/2020",    10,     19,       0,          5,
              "Thailand",  "2/02/2020",    11,     19,       0,          5,
              "Thailand",  "3/02/2020",    12,     19,       0,          5,
              "Thailand",  "4/02/2020",    13,     25,       0,          5,
              "Thailand",  "5/02/2020",    14,     25,       0,          5,
              "Thailand",  "6/02/2020",    15,     25,       0,          5,
              "Thailand",  "7/02/2020",    16,     25,       0,          5,
              "Thailand",  "8/02/2020",    17,     32,       0,         10,
              "Thailand",  "9/02/2020",    18,     32,       0,         10,
              "Thailand", "10/02/2020",    19,     32,       0,         10,
              "Thailand", "11/02/2020",    20,     33,       0,         10,
              "Thailand", "12/02/2020",    21,     33,       0,         10,
              "Thailand", "13/02/2020",    22,     33,       0,         12,
              "Thailand", "14/02/2020",    23,     33,       0,         12,
              "Thailand", "15/02/2020",    24,     33,       0,         12,
              "Thailand", "16/02/2020",    25,     34,       0,         14,
              "Thailand", "17/02/2020",    26,     35,       0,         15,
              "Thailand", "18/02/2020",    27,     35,       0,         15,
              "Thailand", "19/02/2020",    28,     35,       0,         15,
              "Thailand", "20/02/2020",    29,     35,       0,         15,
              "Thailand", "21/02/2020",    30,     35,       0,         17,
              "Thailand", "22/02/2020",    31,     35,       0,         17,
              "Thailand", "23/02/2020",    32,     35,       0,         21,
              "Thailand", "24/02/2020",    33,     35,       0,         21,
              "Thailand", "25/02/2020",    34,     37,       0,         22,
              "Thailand", "26/02/2020",    35,     40,       0,         22,
              "Thailand", "27/02/2020",    36,     40,       0,         22,
              "Thailand", "28/02/2020",    37,     41,       0,         28,
              "Thailand", "29/02/2020",    38,     42,       0,         28,
              "Thailand",  "1/03/2020",    39,     42,       1,         28,
                    "UK", "31/01/2020",     0,      2,       0,          0,
                    "UK",  "1/02/2020",     1,      2,       0,          0,
                    "UK",  "2/02/2020",     2,      2,       0,          0,
                    "UK",  "3/02/2020",     3,      2,       0,          0,
                    "UK",  "4/02/2020",     4,      2,       0,          0,
                    "UK",  "5/02/2020",     5,      2,       0,          0,
                    "UK",  "6/02/2020",     6,      2,       0,          0,
                    "UK",  "7/02/2020",     7,      3,       0,          0,
                    "UK",  "8/02/2020",     8,      3,       0,          0,
                    "UK",  "9/02/2020",     9,      3,       0,          0,
                    "UK", "10/02/2020",    10,      8,       0,          0,
                    "UK", "11/02/2020",    11,      8,       0,          0,
                    "UK", "12/02/2020",    12,      9,       0,          1,
                    "UK", "13/02/2020",    13,      9,       0,          1,
                    "UK", "14/02/2020",    14,      9,       0,          1,
                    "UK", "15/02/2020",    15,      9,       0,          1,
                    "UK", "16/02/2020",    16,      9,       0,          8,
                    "UK", "17/02/2020",    17,      9,       0,          8,
                    "UK", "18/02/2020",    18,      9,       0,          8,
                    "UK", "19/02/2020",    19,      9,       0,          8,
                    "UK", "20/02/2020",    20,      9,       0,          8,
                    "UK", "21/02/2020",    21,      9,       0,          8,
                    "UK", "22/02/2020",    22,      9,       0,          8,
                    "UK", "23/02/2020",    23,      9,       0,          8,
                    "UK", "24/02/2020",    24,     13,       0,          8,
                    "UK", "25/02/2020",    25,     13,       0,          8,
                    "UK", "26/02/2020",    26,     13,       0,          8,
                    "UK", "27/02/2020",    27,     15,       0,          8,
                    "UK", "28/02/2020",    28,     20,       0,          8,
                    "UK", "29/02/2020",    29,     23,       0,          8,
                    "UK",  "1/03/2020",    30,     36,       0,          8,
  "United Arab Emirates", "29/01/2020",     0,      4,       0,          0,
  "United Arab Emirates", "30/01/2020",     1,      4,       0,          0,
  "United Arab Emirates", "31/01/2020",     2,      4,       0,          0,
  "United Arab Emirates",  "1/02/2020",     3,      4,       0,          0,
  "United Arab Emirates",  "2/02/2020",     4,      5,       0,          0,
  "United Arab Emirates",  "3/02/2020",     5,      5,       0,          0,
  "United Arab Emirates",  "4/02/2020",     6,      5,       0,          0,
  "United Arab Emirates",  "5/02/2020",     7,      5,       0,          0,
  "United Arab Emirates",  "6/02/2020",     8,      5,       0,          0,
  "United Arab Emirates",  "7/02/2020",     9,      5,       0,          0,
  "United Arab Emirates",  "8/02/2020",    10,      7,       0,          0,
  "United Arab Emirates",  "9/02/2020",    11,      7,       0,          0,
  "United Arab Emirates", "10/02/2020",    12,      8,       0,          0,
  "United Arab Emirates", "11/02/2020",    13,      8,       0,          0,
  "United Arab Emirates", "12/02/2020",    14,      8,       0,          1,
  "United Arab Emirates", "13/02/2020",    15,      8,       0,          1,
  "United Arab Emirates", "14/02/2020",    16,      8,       0,          1,
  "United Arab Emirates", "15/02/2020",    17,      8,       0,          3,
  "United Arab Emirates", "16/02/2020",    18,      9,       0,          4,
  "United Arab Emirates", "17/02/2020",    19,      9,       0,          4,
  "United Arab Emirates", "18/02/2020",    20,      9,       0,          4,
  "United Arab Emirates", "19/02/2020",    21,      9,       0,          4,
  "United Arab Emirates", "20/02/2020",    22,      9,       0,          4,
  "United Arab Emirates", "21/02/2020",    23,      9,       0,          4,
  "United Arab Emirates", "22/02/2020",    24,     13,       0,          4,
  "United Arab Emirates", "23/02/2020",    25,     13,       0,          4,
  "United Arab Emirates", "24/02/2020",    26,     13,       0,          4,
  "United Arab Emirates", "25/02/2020",    27,     13,       0,          4,
  "United Arab Emirates", "26/02/2020",    28,     13,       0,          4,
  "United Arab Emirates", "27/02/2020",    29,     13,       0,          4,
  "United Arab Emirates", "28/02/2020",    30,     19,       0,          5,
  "United Arab Emirates", "29/02/2020",    31,     21,       0,          5,
  "United Arab Emirates",  "1/03/2020",    32,     21,       0,          5
  )

Which is this:

# A tibble: 101 x 6
   country  obsv_date   days cases deaths recovered
   <chr>    <chr>      <dbl> <dbl>  <dbl>     <dbl>
 1 Thailand 25/01/2020     3     7      0         0
 2 Thailand 26/01/2020     4     8      0         2
 3 Thailand 27/01/2020     5     8      0         2
 4 Thailand 28/01/2020     6    14      0         5
 5 Thailand 29/01/2020     7    14      0         5
 6 Thailand 30/01/2020     8    14      0         5
 7 Thailand 31/01/2020     9    19      0         5
 8 Thailand 1/02/2020     10    19      0         5
 9 Thailand 2/02/2020     11    19      0         5
10 Thailand 3/02/2020     12    19      0         5
# ... with 91 more rows

This code:

covid19 %>%   
  group_by(country) %>%
  summarise(total_cases = sum(cases), total_deaths = sum(deaths), total_recovered = sum(recovered))

Will give you this:

# A tibble: 3 x 4
  country              total_cases total_deaths total_recovered
  <chr>                      <dbl>        <dbl>           <dbl>
1 Thailand                    1058            1             445
2 UK                           280            0             124
3 United Arab Emirates         301            0              69

And this will work even with for all the countries you have because it runs the summary based on the grouping (the country).

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.