R:如何使用每日数据对每周一周数据进行下采样?

时间:2018-04-10 16:56:57

标签: r time-series

在使用Pandas的Python中,您可以使用df.resample(' W-MON')。ffill()来完成。 R中的等价物是什么?

到目前为止,我有这个,但它使用周末数据重新采样,并且代码看起来不太可读:

downsample_weekly <- function(tbl, DATE) {
  ds_ts <- xts(tbl, order.by = as.POSIXct(tbl$DATE))
  as_tibble(ds_ts[endpoints(ds_ts, "weeks")])
}

1 个答案:

答案 0 :(得分:1)

您想在某些数据字段中提取与星期一相对应的tibble行吗?

library(tidyverse)
library(lubridate)

# Example data
foo <- data_frame(Date = seq(ymd("2018-01-01"), ymd("2018-01-31"), length.out = 31),
                  Bar = runif(31))

foo %>% 
  filter(weekdays(Date) == "Monday")

#> # A tibble: 5 x 2
#>   Date         Bar
#>   <date>     <dbl>
#> 1 2018-01-01 0.865
#> 2 2018-01-08 0.119
#> 3 2018-01-15 0.778
#> 4 2018-01-22 0.440
#> 5 2018-01-29 0.550

不清楚您的代码使用xts的原因。

相关问题