在 R 中按两周分组时间序列数据

问题描述 投票:0回答:1

我有 10 年的时间序列数据,我想创建每周、每月和每两周的数据平均值。我每周和每月都这样做,而且效果很好。但我做两周有困难。

示例代码:

# Load necessary packages
library(lubridate) # for handling dates
library(dplyr)     # for data manipulation

# Create a sample dataset with multiple years of timeseries data
dates <- seq(as.Date("2015-06-06"), as.Date("2022-18-12"), by = "day")
values <- rnorm(length(dates), mean = 100, sd = 10)
data <- data.frame(date = dates, value = values)

# Group data by fortnight
grouped_data <- data %>%
  mutate(fortnight = lubridate::floor_date(date, unit = "2 weeks")) %>%
  group_by(fortnight) %>%
  summarise(mean_value = mean(value))

# Print the resulting dataset
print(data)

这给出了错误:

Error in `mutate()`:
i In argument: `fortnight = floor_date(date, unit = "2 weeks")
  - ...`.
Caused by error in `validate_rounding_nunit()`:
! Rounding with week > 1 is not supported. Use aseconds for arbitrary units.
Run `rlang::last_error()` to see where the error occurred.

例如,每周执行一次,此代码有效:

grouped_data <- data %>%
  group_by(value, lubridate::week(date)) 

但是lubridate中没有'fortnight'命令。如果能根据一年中的时间按两周简单地对数据进行分组,我们将不胜感激!

r time-series lubridate
1个回答
1
投票

您可以使用

floor_date(date, 'year')
找到年初,然后使用
week(date) %/% 2
找到要添加的住宿天数。只需将第二个值乘以 14,然后将其加到一年的第一天,即可得出每两周一次的分组日期:

data %>%
  mutate(fortnight = floor_date(date, 'year') + 14 * (week(date) %/% 2)) %>%
  group_by(fortnight) %>%
  summarise(mean_value = mean(value))
#> # A tibble: 205 x 2
#>    fortnight  mean_value
#>    <date>          <dbl>
#>  1 2015-06-04       98.9
#>  2 2015-06-18       98.2
#>  3 2015-07-02       99.1
#>  4 2015-07-16      103. 
#>  5 2015-07-30      100. 
#>  6 2015-08-13      100. 
#>  7 2015-08-27       99.4
#>  8 2015-09-10      101. 
#>  9 2015-09-24       98.5
#> 10 2015-10-08       98.8
#> # ... with 195 more rows
#> # i Use `print(n = ...)` to see more rows
© www.soinside.com 2019 - 2024. All rights reserved.