0
totdal_deaths_confirmed_cases_60_days_only = swine_flu_cases %>% 
filter(Confirmed >0) %>% 
group_by(Country) %>% 
summarise(top_n((total_confirmed_cases = sum(Confirmed), total_deaths = sum(Deaths),60))

So I have a dataframe called swine_flu_cases, and have variables such as:

Country Date Confirmed Recovered Death

What I am trying to do is I want to sum up the groups confirmed and deaths variables in the data frame but only for the first 60 rows/entries per country. I tried using the top_n function but I am not too sure how to apply it into my dataframe before I do the summary. I also tried using slice_max function but my pc doesn't seem to have the function installed even though I loaded the dplyr package so I can't quite figure that out either.

Any suggestions on how I could accomplish this would be appreciated

UseR10085
  • 7,120
  • 3
  • 24
  • 54
DRC_mami
  • 3
  • 3
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 08 '20 at 06:42
  • Welcome to SO! You need the first 60 rows regardless any ordering variable or you need the first 60 rows ordering by date for example? – s__ Sep 08 '20 at 06:47
  • Do you want the top total_confirmed_cases or total_deaths though? – xwhitelight Sep 08 '20 at 06:47
  • I managed to get it. – DRC_mami Sep 08 '20 at 06:52
  • I first extracted the 60 rows that I wanted for my variable and then I summed them up – DRC_mami Sep 08 '20 at 06:53

1 Answers1

0
step_1 = swine_flu_cases %>% 
filter(Confirmed >0)

step_2 = step_1 %>% 
group_by(Country) 
%>% top_n(-60,Date)

Then for the final phase

totdal_deaths_confirmed_cases_60_days_only = summarise(total_confirmed_cases = sum(Confirmed), total_deaths = sum(Deaths))

I don't know if anyone has a much shorter way of doing this, but I did it in steps so that it was easier for me to understand

DRC_mami
  • 3
  • 3