[英]R time series dataset - Lag date and time - Irregular data periods
我正在努力處理包含date和time的數據集。 我想建立新的列,例如same_period_previous_week和same_period_previous_day 。
我已經在 Stackoverflow 中閱讀了幾個答案,但無法修復它。
這是重新創建我的數據集的代碼:
structure(list(date = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("8/19/2018", "8/25/2018", "8/26/2018" ), class = "factor"), time = c(9L, 10L, 11L, 12L, 10L, 11L, 12L, 10L, 11L, 12L), value = c(2L, 15L, 25L, 35L, 10L, 20L, 30L, 7L, 14L, 21L)), .Names = c("date", "time", "value"), class = "data.frame", row.names = c(NA, -10L))
看起來像這樣:
date time value
8/26/2018 9 2
8/26/2018 10 15
8/26/2018 11 25
8/26/2018 12 35
8/25/2018 10 10
8/25/2018 11 20
8/25/2018 12 30
8/19/2018 10 7
8/19/2018 11 14
8/19/2018 12 21
我嘗試使用dplyr ,首先對數據集進行排序,然后對其進行分組並創建一個滯后列。 這是我的代碼:
df <- df %>% arrange(date, time)
df_tmp <- df %>% group_by(date, time) %>% mutate(lag_1day = lag(value, n = 1, default = NA))
新列 (lag_1day) 以幾個 NA 結束。
我希望得到以下結果:
date time value lag_1day
8/26/2018 9 2 NA
8/26/2018 10 15 10
8/26/2018 11 25 20
8/26/2018 12 35 30
8/25/2018 10 10 7
8/25/2018 11 20 14
8/25/2018 12 30 21
8/19/2018 10 7 NA
8/19/2018 11 14 NA
8/19/2018 12 21 NA
請注意,第一行有一個 NA,因為前一天上午 9 點沒有相應的值。
我在第一步中是按升序還是降序排列它們有關系嗎?
感謝期待!
像這樣嘗試。 我想你快到了。
library(dplyr)
df$date <- as.Date(df$date, '%m/%d/%Y')
df %>%
arrange(time, date) %>%
group_by(time) %>%
mutate(lag_1day = lag(value, n = 1, default = NA)) %>%
arrange(desc(date, time))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.