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
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!