【发布时间】:2019-05-22 10:29:44
【问题描述】:
如果我有一个数据框,我希望根据时间范围选择一个训练集
df <- data.frame(timestamp = seq(as.POSIXct('2013-08-02 12:00:00'),
as.POSIXct('2013-08-06 05:00:00'), len =(45),
x = sample(1:100, 45), y = sample(200:500, 45)))
我现在将时间戳转换为 row.names
row.names(df) = df$timestamp
既然我已经索引了 row.names,我应该能够为训练集选择一个范围:
# Select the range
s = '2013-08-02 12:00:00'
e = '2013-08-03 10:15:00'
# Select the training dataset
training = df[s:e,]
但是当我运行上面的代码时,我得到了以下错误:
#Error in s:e : NA/NaN argument
#In addition: Warning messages:
#1: In `[.data.frame`(df, s:e, ) : NAs introduced by coercion
#2: In `[.data.frame`(df, s:e, ) : NAs introduced by coercion
谁能解释一下我在这里做错了什么!
我知道 ts 或其他一些软件包可以解决这个问题,但没有任何我可以使用的基本 R 函数。
我在发布问题之前查看的答案。
【问题讨论】: