从分钟到每日值返回NA

时间:2019-11-26 16:36:58

标签: r dplyr

我每分钟收到一次数据,我想计算该值的每日变化。

我正在尝试:

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

1 个答案:

答案 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))