r 两个 xts 操作 [英] r two xts operations
本文介绍了r 两个 xts 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
两个小时的时间序列 xts1 和 xts2,xts1 有一些缺失的时间.
Two hourly time series xts1 and xts2, xts1 has some missing times.
xts1
time speed power
2010-01-01 00:00:00 0.1 1.1
2010-01-01 01:00:00 0.2 1.2
2010-01-01 05:00:00 0.2 1.2
.....
xts2
time speed power
2010-01-01 00:00:00 0.1 1.1
2010-01-01 01:00:00 0.2 1.2
2010-01-01 02:00:00 0.2 1.2
.....
当将它们组合成一个文件时(获取速度的平均值,并基于相同的时间戳求和),得到不符合数组错误.使用的命令是:
When combine them into one file(get the average of speed, and sum the power based on the same timestamp), get non--conformable arrays error. The command used was:
hourly.data.table = data.table (time = time(xts1), meanspeed= (coredata(xts1$speed)+coredata(xts2$speed))/2, power= coredata(xts1$power)+coredata(xts2$power))
这个时间组合怎么做?提前致谢.
How to do this combination by time? Thanks in advance.
推荐答案
尝试
library(xts)
xts1 <- xts(df1[-1], order.by = as.POSIXct(df1$time))
xts2 <- xts(df2[-1], order.by = as.POSIXct(df2$time))
res <- xts1+xts2
res[,1] <- res[,1]/2
res
# speed power
#2010-01-01 00:00:00 0.1 2.2
#2010-01-01 01:00:00 0.2 2.4
数据
df1 <- structure(list(time = c("2010-01-01 00:00:00",
"2010-01-01 01:00:00",
"2010-01-01 05:00:00"), speed = c(0.1, 0.2, 0.2), power = c(1.1,
1.2, 1.2)), .Names = c("time", "speed", "power"),
class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(time = c("2010-01-01 00:00:00",
"2010-01-01 01:00:00",
"2010-01-01 02:00:00"), speed = c(0.1, 0.2, 0.2), power = c(1.1,
1.2, 1.2)), .Names = c("time", "speed", "power"),
class = "data.frame", row.names = c(NA, -3L))
这篇关于r 两个 xts 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文