r - 填写缺失的时间步长 (yyyy-mm-dd HH :MM:SS) by adding rows with missing times in R

标签 r datetime time-series xts zoo

我有一个大型数据集,如下所示:

Time,Volume    
1996-02-05 00:34:00,0.01
1996-02-05 00:51:00,0.01
1996-02-05 00:52:00,0.01
1996-02-05 01:04:00,0.01
1996-02-05 01:19:00,0.01
1996-02-05 05:00:00,0.01
1996-02-05 05:07:00,0.01
1996-02-05 05:08:00,0.01
1996-02-05 05:14:00,0.01

我想对每 30 分钟间隔的 Volume 列进行求和。这是我尝试过的:

z <- read.zoo("precip.csv", header = TRUE, sep = ",", FUN = as.chron)
half_hour <- period.apply(z, endpoints(z, "minutes", 30), length)

返回:

Time,Volume
02/05/96 00:52:00,3
02/05/96 01:19:00,2
02/05/96 05:14:00,4

我试图让输出看起来像:

Time,Volume
02/05/96 00:29:00,0
02/05/96 00:59:00,3
02/05/96 01:29:00,2
02/05/96 01:59:00,0
02/05/96 02:29:00,0
02/05/96 02:59:00,0

...等等。

或者,我认为如果我可以填写原始数据集,以便计算每一分钟(其中缺少的Volumes 等于 0),那么它会起作用。

我找到了this post ,但无法使其工作。

> z_xts<- xts(precip[,c("Volume")],precip[,"Time"])
Error in xts(precip[, c("Volume")], precip[, "Time"]) : 
  order.by requires an appropriate time-based object

最佳答案

这应该可以满足您的要求:

library(xts)
x <- as.xts(read.zoo(text="Time,Volume    
1996-02-05 00:34:00,0.01
1996-02-05 00:51:00,0.01
1996-02-05 00:52:00,0.01
1996-02-05 01:04:00,0.01
1996-02-05 01:19:00,0.01
1996-02-05 05:00:00,0.01
1996-02-05 05:07:00,0.01
1996-02-05 05:08:00,0.01
1996-02-05 05:14:00,0.01",
sep=",", FUN=as.POSIXct, header=TRUE, drop=FALSE))

# 1) Create POSIXct sequence from midnight of the first day
#    until the end of the last day    
midnightDay1 <- as.POSIXct(format(start(x),"%Y-%m-%d"))
timesteps <- seq(midnightDay1, end(x), by="30 min")
# 2) Make a copy of your object and set all values for Volume to 1
y <- x
y$Volume <- 1
# 3) Merge the copy with a zero-column xts object that has an index
#    with all the values you want.  Fill missing values with 0.
m <- merge(y, xts(,timesteps), fill=0)
# 4) Align all index values to 30-minute intervals
a <- align.time(m, 60*30)
# 5) Sum the values for Volume in each period
half_hour <- period.apply(a, endpoints(a, "minutes", 30), sum)

关于r - 填写缺失的时间步长 (yyyy-mm-dd HH :MM:SS) by adding rows with missing times in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931428/

相关文章:

python - Pandas 使用另一列的值移动日期

python - UnboundLocalError : local variable 'x' referenced before assignment. 在数据帧的 seaborn 包中正确使用 tsplot?

r - 尝试从 github 安装 R fst 包的开发版本时出现 "/bin/sh: XX: command not found"错误

javascript - 如何显示hh :mm:ss format at y-axis with c3?

php - MySQL:SUBTIME()没有负值?

python - 如何在 pandas 数据框中执行不同值的累积和

r - R中的加权平均值

r - R 中相同字符串的不同 md5 哈希值

r - rowSums,但保留NA值

r - R(NLP包)中的方法注释是否已被弃用或替换?