zoo

alignment and offsets in rollapply

不打扰是莪最后的温柔 提交于 2020-01-03 13:10:28
问题 I am trying to calculate some statistics for a moving window and am using rollapply in the zoo package. My question is how do I get rollapply to apply that function to the previous n observations instead of the current observation and the previous n-1 observations as align right seems to do. require(zoo) z <- data.frame(x1=11:111, x2=111:211, x3=as.Date(31:131))#generate data output<-data.frame(dates=z$x3, rollapply(z[,1:2],by.column=TRUE, 5, max, fill=NA, align='right')) I have a hunch this

Aggregate (count) occurences of values over arbitrary timeframe

点点圈 提交于 2020-01-02 06:55:10
问题 I have a CSV file with timestamps and certain event-types which happened at this time. What I want is count the number of occurences of certain event-types in 6-minutes intervals. The input-data looks like: date,type "Sep 22, 2011 12:54:53.081240000","2" "Sep 22, 2011 12:54:53.083493000","2" "Sep 22, 2011 12:54:53.084025000","2" "Sep 22, 2011 12:54:53.086493000","2" I load and cure the data with this piece of code: > raw_data <- read.csv('input.csv') > cured_dates <- c(strptime(raw_data$date,

Is there any way to force zoo::rollmean function to return a vector that is the same length as it's input? (or maybe use other function?)

余生长醉 提交于 2020-01-02 05:29:45
问题 input = cbind(c(3,7,3,5,2,9,1,4,6,4,7,3,7,4)) library(zoo) output = cbind(rollmean(input,4)) print(input) print(output) output: [,1] [1,] 3 [2,] 7 [3,] 3 [4,] 5 [5,] 2 [6,] 9 [7,] 1 [8,] 4 [9,] 6 [10,] 4 [11,] 7 [12,] 3 [13,] 7 [14,] 4 [,1] [1,] 4.50 [2,] 4.25 [3,] 4.75 [4,] 4.25 [5,] 4.00 [6,] 5.00 [7,] 3.75 [8,] 5.25 [9,] 5.00 [10,] 5.25 [11,] 5.25 but when I try to cbind it: Error in cbind(input, output) : number of rows of matrices must match (see arg 2) Calls: print -> cbind Execution

Plot value over hour of day with xts/zoo R

余生颓废 提交于 2020-01-01 15:35:10
问题 I do have a time-series looking like this (minute values): "timestamp", value "2012-04-09 05:03:00",2 "2012-04-09 05:04:00",4 "2012-04-09 05:05:00",5 "2012-04-09 05:06:00",0 "2012-04-09 05:07:00",0 "2012-04-09 05:08:00",3 "2012-04-09 05:09:00",0 "2012-04-09 05:10:00",1 Is there an easy way to plot these values over the hour of the day: X-Axis from 1 to 24 hours (or 0 and 23). So - all values between 5:00 and 5:59 over the 5th hour etc. It doesn't depend wich date, I am just interested in the

Converting zoo to ts before forecasting

梦想与她 提交于 2020-01-01 05:23:08
问题 I am struggling to convert a zoo objects to a ts object. I have a huge data.frame "test" with quarterly hour data, which looks like this: date <- c("2010-07-04 09:45:00", "2010-07-04 10:00:00", "2010-07-04 10:15:00", "2010-07-04 10:30:00", "2010-07-04 10:45:00", "2010-07-04 11:00:00") nrv <- c("-147.241", "-609.778", "-432.289", "-340.418", "-73.96" , "-533.108") tt <- c("3510.7", "3608.5", "3835.7", "4003.7", "4018.8", "4411.9") test <- data.frame(date,nrv,tt) test I want to make some

How do I match data frames by time intervals?

こ雲淡風輕ζ 提交于 2020-01-01 03:33:06
问题 This is a problem that comes up often when I am importing raw data from data loggers. A temperature logger is set to record temperature every ten minutes, and a separate gas logger is set to record gas used in the last ten minute interval. I want to combine the data from these two loggers into a single data frame for plotting and analysis, but the times are not exactly aligned. I want to have one row in the data frame for each ten minute period, with the datetime showing the beginning of the

Converting yearmon column to last date of the month in R

陌路散爱 提交于 2019-12-31 03:06:39
问题 I have a data frame (df) like the following: Date Arrivals 2014-07 100 2014-08 150 2014-09 200 I know that I can convert the yearmon dates to the first date of each month as follows: df$Date <- as.POSIXct(paste0(as.character(df[,1]),"-01"), format = "%Y-%m-%d") However, given that my data is not available until the end of the month I want to index it to the end rather than the beginning, and I cannot figure it out. Any help appreciated. 回答1: If the Date variable is an actual yearmon class

Using rollapply function for VaR calculation using R

僤鯓⒐⒋嵵緔 提交于 2019-12-31 02:34:09
问题 I did the following for calculating Value at Risk (VaR) over 20 period rolling window: require(PerformanceAnalytics); require(zoo) data(edhec) class(edhec) # [1] "xts" "zoo" class(edhec$CTAGlobal) # "NULL" var1<-rollapply(edhec,width=20,FUN=function(edhec) VaR(R=edhec,p=.95,method="modified"),by.column=TRUE) It produces the desired output, and then I tried the same on another data: data(managers) class(managers) # [1] "xts" "zoo" class(managers$HAM4) # [1] "xts" "zoo" var2<-rollapply(managers

rolling computations in xts by month

我与影子孤独终老i 提交于 2019-12-30 10:50:54
问题 I am familiar with the zoo function rollapply which allows you to do rolling computations on zoo or xts objects and you can specify the rolling increment via the by parameter. I am specifically interested in applying a function every month but using all of the past daily data in the computation. For example say my data set looks like this: dte, val 1/01/2001, 10 1/02/2001, 11 ... 1/31/2001, 2 2/01/2001, 54 2/02/2001, 34 ... 2/30/2001, 29 I would like to select the end of each month and apply

na.locf but don't do trailing NAs

假装没事ソ 提交于 2019-12-30 08:23:57
问题 I have the following time series > y<- xts(1:10, Sys.Date()+1:10) > y[c(1,2,5,9,10)] <- NA > y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 2011-09-07 4 2011-09-08 NA 2011-09-09 6 2011-09-10 7 2011-09-11 8 2011-09-12 NA 2011-09-13 NA A straight na.locf give me this: > na.locf(y) [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 2011-09-07 4 2011-09-08 4 2011-09-09 6 2011-09-10 7 2011-09-11 8 2011-09-12 8 2011-09-13 8 how do i get to this? [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 2011-09-07