名为 d 的数据框包含以下数据:
timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70
我的代码是:
d = read.csv(path, header=TRUE, sep=",")
d2 <- xts(x = d[c("value")], order.by = as.POSIXct(d[, "timestamp"], tz = "GMT", format = "%Y-%m-%d %H:%M:%S"))
ends <- endpoints(d2, on = "minutes", k = 15)
d3 <- period.apply(d2, ends, mean)
之后,我想将 xts 对象转换为数据框,我正在使用它:
d3$timestamp = rownames(d3)
rownames(d3) = NULL
d3$timestamp = strptime(d3$timestamp, "%Y-%m-%d %H:%M:%S")
但是在最后一步中,它会向此打印错误:
Error in NextMethod(.Generic) :
number of items to replace is not a multiple of replacement length
当我观察到在整个命令之后键入 d3 时,该对象具有以下数据格式:
timestamp
2013-06-02 00:15:00 65
2013-06-02 00:30:00 70
2013-06-02 00:45:00 70
2013-06-02 01:00:00 70
但是,在列名中,它必须具有名称值,并且第二列具有像这里一样的时间戳。有什么问题?
正确的输出必须是这样的:
value
65 2013-06-02 00:15:00
70 2013-06-02 00:30:00
70 2013-06-02 00:45:00
70 2013-06-02 01:00:00