延长 R 中 data.frames 的日期?
extend dates of a data.frames in R?
我想 extend
DF1
的 Datetime
直到 End_date
变量 x
的最后一个值被重复直到 End_date
。如有任何建议,我们将不胜感激。
library(lubridate)
set.seed(123)
DF1 <- data.frame(Datetime = seq(ymd_hms("2011-01-01 00:00:00"), to= ymd_hms("2011-01-10 00:00:00"), by = "hour"),
X = runif(217, 5,10))
End_date <- "2011-01-15 00:00:00"
示例数据,只需几行即可演示:
set.seed(123)
DF1 <- data.frame(
Datetime = seq(as.POSIXct("2011-01-01 00:00:00"), to = as.POSIXct("2011-01-01 03:00:00"), by = "hour"),
X = runif(4, 5, 10)
)
End_date <- as.POSIXct("2011-01-01 07:00:00")
DF1
# Datetime X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087
add-on 框架只是对 data.frame
/seq
的另一个调用,使用来自 DF1
:
的最后一个值
DF1aug <- data.frame(
Datetime = seq(from = DF1$Datetime[nrow(DF1)], to = End_date, by="hour")[-1],
X = DF1$X[nrow(DF1)]
)
DF1aug
# Datetime X
# 1 2011-01-01 04:00:00 9.415087
# 2 2011-01-01 05:00:00 9.415087
# 3 2011-01-01 06:00:00 9.415087
# 4 2011-01-01 07:00:00 9.415087
这可以很容易地增加到第一个 rbind
:
rbind(DF1, DF1aug)
# Datetime X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087
# 5 2011-01-01 04:00:00 9.415087
# 6 2011-01-01 05:00:00 9.415087
# 7 2011-01-01 06:00:00 9.415087
# 8 2011-01-01 07:00:00 9.415087
如果您使用的是 tidyverse,那么所有这些都与:
library(dplyr)
DF1 %>%
slice(n()) %>%
do(data.frame(
Datetime = seq(from = .$Datetime, to = End_date, by="hour")[-1],
X = .$X
)) %>%
bind_rows(DF1, .)
您可以将 complete
与 fill
一起使用:
library(tidyr)
DF1 %>%
complete(Datetime = seq(min(Datetime), as.POSIXct(End_date), 'hour')) %>%
fill(X)
或者仅使用 complete
我们可以在 fill
中传递 DF1$X
的最后一个值。
DF1 %>%
complete(Datetime = seq(min(Datetime), as.POSIXct(End_date), 'hour'),
fill = list(X = DF1$X[nrow(DF1)]))
我想 extend
DF1
的 Datetime
直到 End_date
变量 x
的最后一个值被重复直到 End_date
。如有任何建议,我们将不胜感激。
library(lubridate)
set.seed(123)
DF1 <- data.frame(Datetime = seq(ymd_hms("2011-01-01 00:00:00"), to= ymd_hms("2011-01-10 00:00:00"), by = "hour"),
X = runif(217, 5,10))
End_date <- "2011-01-15 00:00:00"
示例数据,只需几行即可演示:
set.seed(123)
DF1 <- data.frame(
Datetime = seq(as.POSIXct("2011-01-01 00:00:00"), to = as.POSIXct("2011-01-01 03:00:00"), by = "hour"),
X = runif(4, 5, 10)
)
End_date <- as.POSIXct("2011-01-01 07:00:00")
DF1
# Datetime X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087
add-on 框架只是对 data.frame
/seq
的另一个调用,使用来自 DF1
:
DF1aug <- data.frame(
Datetime = seq(from = DF1$Datetime[nrow(DF1)], to = End_date, by="hour")[-1],
X = DF1$X[nrow(DF1)]
)
DF1aug
# Datetime X
# 1 2011-01-01 04:00:00 9.415087
# 2 2011-01-01 05:00:00 9.415087
# 3 2011-01-01 06:00:00 9.415087
# 4 2011-01-01 07:00:00 9.415087
这可以很容易地增加到第一个 rbind
:
rbind(DF1, DF1aug)
# Datetime X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087
# 5 2011-01-01 04:00:00 9.415087
# 6 2011-01-01 05:00:00 9.415087
# 7 2011-01-01 06:00:00 9.415087
# 8 2011-01-01 07:00:00 9.415087
如果您使用的是 tidyverse,那么所有这些都与:
library(dplyr)
DF1 %>%
slice(n()) %>%
do(data.frame(
Datetime = seq(from = .$Datetime, to = End_date, by="hour")[-1],
X = .$X
)) %>%
bind_rows(DF1, .)
您可以将 complete
与 fill
一起使用:
library(tidyr)
DF1 %>%
complete(Datetime = seq(min(Datetime), as.POSIXct(End_date), 'hour')) %>%
fill(X)
或者仅使用 complete
我们可以在 fill
中传递 DF1$X
的最后一个值。
DF1 %>%
complete(Datetime = seq(min(Datetime), as.POSIXct(End_date), 'hour'),
fill = list(X = DF1$X[nrow(DF1)]))