0

I have data similar to this Sample Data:

   Cities Country    Date Cases
1      BE       A 2/12/20    12
2      BD       A 2/12/20   244
3      BF       A 2/12/20     1
4               V 2/12/20    13
5               Q 2/13/20     2
6               D 2/14/20     4
7      GH       N 2/15/20     6
8      DA       N 2/15/20   624
9      AG       J 2/15/20   204
10     FS       U 2/16/20   433
11     FR       U 2/16/20    38

I want to organize the data by on the date and country and then sum a country's daily case. However, I try something like, it reveal the total sum:

my_data %>%
  group_by(Country, Date)%>%
  summarize(Cases=sum(Cases))
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
qqqoooeee
  • 1
  • 1
  • welcome to SO. please provide a minimal reproducible example with example data and the expected output: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – starja May 29 '20 at 14:15

2 Answers2

1

Your summarize function is likely being called from another package (plyr?). Try calling dplyr::sumarize like this:

my_data %>%
  group_by(Country, Date)%>%
  dplyr::summarize(Cases=sum(Cases))
# A tibble: 7 x 3
# Groups:   Country [7]
  Country Date    Cases
  <fct>   <fct>   <int>
1 A       2/12/20   257
2 D       2/14/20     4
3 J       2/15/20   204
4 N       2/15/20   630
5 Q       2/13/20     2
6 U       2/16/20   471
7 V       2/12/20    13

I sympathize with you that this is can be very frustrating. I have gotten into a habit of always using dplyr::select, dplyr::filter and dplyr::summarize. Otherwise you spend needless time frustrated about why your code isn't working.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
0

We can also use aggregate

aggregate(Cases ~ Country + Date, my_data, sum)
akrun
  • 874,273
  • 37
  • 540
  • 662