Investigating the Coronavirus Pandemic - Off-Platform project - extra challenges

Hi! I’m working with the Analyse Data with R: coronavirus off-platform project and am having touble with the extra challenges towards the end of the project.

The first extra challenge says:

  • Find the rate of change. Instead of reporting the total number of confirmed cases in a country, report how many new cases there were that day. Which countries are slowing their rate of infection?

I am having trouble converting the regular df with ‘countries’ as column heads and ‘days’ as rows containing the confirmed cases of covid on a particular date, into a df where each element is the rate of change of infection.

I tried the PercChange function in some forms but it did not work.

df_pct_change <- PercChange(df, Var = "", type= "percent", slideBy = -1)

I want percentage change throughout the dataframe and not a particular column due to which I’m not sure what to type as a string character for Var in the PercChange function.

I had earlier tried manually finding the growth rates by doing the following:

df_pct_change <- df %>% transmute(pct_change = 100 * (df - lag(df) / lag(df)))

However, I got a column of pct_change which had dataframes for every row, by country. I read that the melt() function could be used on that but nothing is working really. It is a very silly doubt but I’m having much trouble going forward with this. Please help me if you can!

Hi! I’m not sure I fully understand what you’re using as df in the code above, so this answer may not be exactly what you are looking for – but if you start with the dataframe transposed_confirmed, which you can get from the corona_confirmed.csv using the following (this code is in the solution file, but broken up into smaller pieces):

library(janitor)
library(dplyr)
transposed_confirmed < - countries_confirmed 
%>% t() 
%>% as.data.frame() 
%>% row_to_names(row_number = 1) 
%>% apply(transposed_confirmed, 2, as.numeric)

Then you can use transposed_confirmed to get a new dataframe where the columns are percent changes using:

df_pct_change < - transposed_confirmed 
%>% transmute_all(funs(pct_change = 100 * ((. - lag(.)) / lag(.))))
print(df_pct_change)

Hopefully that helps! Let me know if anything is still unclear :slight_smile:

2 Likes

This worked, thank you so much! I wasn’t aware of how the dots were used since I’m new to R. The transmute_all function was new too. Thanks again! :slight_smile:

1 Like