我每分钟收到一次数据,我想计算该值的每日变化。
我正在尝试:
mydf <- tibble(mydate = as.Date(c("2019-05-11 23:55:00", "2019-05-11 23:56:00", "2019-05-11 23:57:00", "2019-05-11 23:58:00",
"2019-05-12 23:55:00", "2019-05-12 23:56:00", "2019-05-12 23:57:00", "2019-05-12 23:58:00")),
myval = c(101.23, 102.34, 105.43, 106.7, 107.1, 108.2, 109.2, 110.1))
mydf <- mydf %>%
group_by(day = floor_date(mydate, "day")) %>%
mutate(Val_dev = ((myval - lag(myval)) / myval) * 100) %>%
summarise(val_change = mean(Val_dev), days = mean(day))
mydf
我收到NA值:
day val_change days
<date> <dbl> <date>
1 2019-05-11 NA 2019-05-11
2 2019-05-12 NA 2019-05-12
答案 0 :(得分:0)
并非每个日期都可以滞后。有些人没有以前的日期。您可以滞后使用default
参数。或na.rm=TRUE
中的mean
。
mydf <- mydf %>%
group_by(day = floor_date(mydate, "day")) %>%
mutate(Val_dev = ((myval - lag(myval)) / myval) * 100) %>%
summarise(val_change = mean(Val_dev, na.rm=T), days = mean(day, na.rm =T))