我有一个datframe,我想每天甚至在三天内计算变化百分比,但是当我这样做时,结果似乎并不正确。
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))
所以我有这个:
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))
我这样做正确吗?我也不知道如何让三天一班的工作。谢谢!
答案 0 :(得分:1)
您可以尝试:
df %>%
mutate_at(.vars = vars(dplyr::matches("(likes)|(comments)")),
funs(daily_change = ./lag(.)*100,
three_day_change = ./lag(., 3)*100))
类似地,如果您不需要ad和date变量:
df %>%
select(likes, comments) %>%
mutate_all(funs(daily_change = ./lag(.)*100,
three_day_change = ./lag(., 3)*100))
或者如果您需要它们:
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)
此外,您可以通过对原始代码进行少量修改来获得相同的结果:
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))