mjoy
mjoy

Reputation: 680

How to calculate daily percent change and three day percent change R?

I have a datframe with that I want to calculate the percent change day by day and also over three days but when I do it the results don't really seem right.

ads <- data.frame(ad = c(ad1, ad1, ad1, ad1, ad2, ad2, ad2, ad3, ad3, ad3), 
                  date = c("11-10", "11-11", "11-12", "11-13", "11-10", "11-11", "11-12", "11-10", "11-11", "11-12"), 
                  likes = c(20, 30, 18, 5, 34, 68, 55, 44, 33, 20),
                  comments = c(21, 22, 10, 1, 10, 43, 24, 34, 21, 11))

so for I have this:

daily_pct <- function(x) x/lag(x)
three_pct <- function(x) x/lag(x ,k = 3)

daily_pct_change <- ads %>%
     mutate_each(funs(daily_pct), c(likes,comments))

three_pct_change <- ads %>% 
     mutate_each(funs(three_pct), c(likes, comments))

Am I doing this correctly? I can't figure out how to get the three day one to work either. Thanks!

Upvotes: 0

Views: 585

Answers (1)

tmfmnk
tmfmnk

Reputation: 39858

You can try:

df %>% 
  mutate_at(.vars = vars(dplyr::matches("(likes)|(comments)")), 
            funs(daily_change = ./lag(.)*100,
                 three_day_change = ./lag(., 3)*100))

Similarly, if you do not need the ad and date variables:

df %>% 
  select(likes, comments) %>% 
  mutate_all(funs(daily_change = ./lag(.)*100,
                 three_day_change = ./lag(., 3)*100))

Or if you need them:

df %>% 
  select(likes, comments) %>% 
  mutate_all(funs(daily_change = ./lag(.)*100,
                 three_day_change = ./lag(., 3)*100)) %>% 
  rowid_to_column() %>% 
  left_join(df %>% rowid_to_column() %>% select(rowid, ad, date), by = c("rowid" = "rowid")) %>%
  select(-rowid)

Also, you can get the same results by a small modification of your original code:

daily_pct <- function(x) x/lag(x)*100
three_pct <- function(x) x/lag(x, 3)*100

df %>% 
  mutate_at(.vars = vars(dplyr::matches("(likes)|(comments)")), 
            funs(daily_change = daily_pct,
                 three_day_change = three_pct))

Upvotes: 1

Related Questions