我试图从数据框中提取一系列POSIXct日期
带有样本数据框(数据)......
library(lubridate)
start <- as.POSIXct('2016-10-08 20:00:00')
time <- seq(start, start + days(1), by = "30 min")
variable <- sample(seq(from = 20, to = 50), size = 49, replace = TRUE)
data <- data.frame(time, variable)
我尝试使用单个日期和范围来创建日期的索引向量。我最初将数据框子集化,然后使用$
运算符提取日期列(尽管我确信有更优雅的方法)。
data_trim <- data[c('2016-10-08 20:00:00', '2016-10-08 22:0:00' %--% '2016-
10-09 13:00:00'),]
但输出是
time variable
NA <NA> NA
NA.1 <NA> NA
有没有人有任何建议?
谢谢
答案 0 :(得分:0)
这是你想要达到的目标吗?如果您在其他地方不需要days(1)
,则可以将60 * 60 * 24
替换为lubridate
(秒*分钟*小时)。
编辑:更改为仅提取时间向量。
start <- as.POSIXct('2016-10-08 20:00:00')
time <- seq(start, start + 60 * 60 * 24, by = "30 min")
variable <- sample(seq(from = 20, to = 50), size = 49, replace = TRUE)
data <- data.frame(time, variable)
# If you wanted to keep the data.frame
# data_trim <- data[data$time == '2016-10-08 20:00:00' |
# (data$time >= '2016-10-08 22:0:00' &
# data$time <= '2016-10-09 13:00:00'), ]
# data_trim
# Otherwise just extract the time column into a vector
data$time[data$time == '2016-10-08 20:00:00' |
(data$time >= '2016-10-08 22:0:00' &
data$time <= '2016-10-09 13:00:00')]
#> [1] "2016-10-08 20:00:00 NZDT" "2016-10-08 22:00:00 NZDT"
#> [3] "2016-10-08 22:30:00 NZDT" "2016-10-08 23:00:00 NZDT"
#> [5] "2016-10-08 23:30:00 NZDT" "2016-10-09 00:00:00 NZDT"
#> [7] "2016-10-09 00:30:00 NZDT" "2016-10-09 01:00:00 NZDT"
#> [9] "2016-10-09 01:30:00 NZDT" "2016-10-09 02:00:00 NZDT"
#> [11] "2016-10-09 02:30:00 NZDT" "2016-10-09 03:00:00 NZDT"
#> [13] "2016-10-09 03:30:00 NZDT" "2016-10-09 04:00:00 NZDT"
#> [15] "2016-10-09 04:30:00 NZDT" "2016-10-09 05:00:00 NZDT"
#> [17] "2016-10-09 05:30:00 NZDT" "2016-10-09 06:00:00 NZDT"
#> [19] "2016-10-09 06:30:00 NZDT" "2016-10-09 07:00:00 NZDT"
#> [21] "2016-10-09 07:30:00 NZDT" "2016-10-09 08:00:00 NZDT"
#> [23] "2016-10-09 08:30:00 NZDT" "2016-10-09 09:00:00 NZDT"
#> [25] "2016-10-09 09:30:00 NZDT" "2016-10-09 10:00:00 NZDT"
#> [27] "2016-10-09 10:30:00 NZDT" "2016-10-09 11:00:00 NZDT"
#> [29] "2016-10-09 11:30:00 NZDT" "2016-10-09 12:00:00 NZDT"
#> [31] "2016-10-09 12:30:00 NZDT" "2016-10-09 13:00:00 NZDT"