我试图在这里关注这篇堆栈文章:如何在R 中的数据帧中按日期每8天获得一次值的总和
有人知道为什么这不起作用吗?
library(xts)
set.seed(123)
property_damages_in_dollars <- rnorm(731,100,10)
date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
other_damages_in_dollars <- rnorm(731,10,10)
final_data <- data.frame(date_decision_made, other_damages_in_dollars, property_damages_in_dollars)
ep <- endpoints(final_data,'days',k=8)
a = period.apply(x=final_data,ep,FUN=sum )
注意:对于两个变量,这个代码可以工作吗?
dat <- xts(cbind(final_data$property_damages_in_dollars, final_data$other_damages_in_dollars),
as.Date(final_data$date_decision_made, '%Y/%m/%d'))
ep <- endpoints(dat,'days',k=8)
a = period.apply(x=dat,ep,FUN=sum )
如果有一个数据帧,请将其更改为xts
对象。
library(xts)
dat <- xts(final_data$property_damages_in_dollars,
as.Date(final_data$date_decision_made, '%Y/%m/%d'))
ep <- endpoints(dat,'days',k=8)
a = period.apply(x=dat,ep,FUN=sum )
略微修改Ronak Shah的代码:
#first variable
dat <- xts(final_data$property_damages_in_dollars,
as.Date(final_data$date_decision_made, '%Y/%m/%d'))
ep <- endpoints(dat,'days',k=8)
a = period.apply(x=dat,ep,FUN=sum )
#second variable
dat <- xts(final_data$other_damages_in_dollars,
as.Date(final_data$date_decision_made, '%Y/%m/%d'))
ep <- endpoints(dat,'days',k=8)
b = period.apply(x=dat,ep,FUN=sum )
#combine - not a very efficient way to solve this
a = data.frame(a)
b = data.frame(b)
c = cbind(a,b)