zoo

Split time-series weekly in R

狂风中的少年 提交于 2019-12-12 03:45:47
问题 I want to split xts/zoo time-series in R on weekly basis. The timezone is set to "Asia/Kolkata" Sys.setenv(TZ="Asia/Kolkata") library(xts) seqs<- seq(as.POSIXct("2016-01-01"),as.POSIXct("2016-01-30"), by = "30 mins") ob<- xts(data.frame(value=1:(length(seqs))),seqs) weekdata <- split(ob,f="weeks",k=1) The problem with this split is that each week data is offset by 5:30 hours as shown below > head(weekdata[[2]],2) value 2016-01-04 05:30:00 156 2016-01-04 06:00:00 157 > head(weekdata[[3]],2)

“Index has bad entries at data rows” error when reading CSV

梦想与她 提交于 2019-12-12 02:29:12
问题 I have a .csv file containing stock prices in the below format Date Open High Low Close 3/7/2014 10:15 228.28 236.58 228.23 236.18 3/7/2014 11:15 236.23 241.27 236.09 241.14 3/7/2014 12:15 241.1 246.12 241.1 242.96 3/7/2014 13:15 242.84 243.92 242 242.32 When I am running the below script: test <- as.xts(read.zoo(mytest.csv', header=T, fill=T, index.column = 1, row.names=NULL,stringsAsFactors = FALSE, sep="",format="%m/%d/%y %H:%M", tz="")) The below error is shown Error in read.zoo("mydata

How do I subset the last week for every month of a zoo object in R?

不问归期 提交于 2019-12-12 01:44:25
问题 I have a few years of daily price data as a zoo object. What's the best way to subset the last week of 'every' month in R? Here's how you can replicate it: set.seed(123) price <- rnorm(365) data <- cbind(seq(as.Date("2013-01-01"), by = "day", length.out = 365), price) zoodata <- zoo(data[,2], as.Date(data[,1])) I tried this option but it only returns the last week of the entire data set. do.call(rbind, lapply(split(zoodata, "months"), last, "1 week")) Resulting output is: 2013-12-30 2013-12

How to calculate moving average by specified grouping and deal with NAs

六月ゝ 毕业季﹏ 提交于 2019-12-11 19:04:09
问题 I have a data.table which needs a moving average to be calculated on the previous n days of data (let's use n=2 for simplicity, not incl. current day) for a specified grouping (ID1, ID2). The moving average should attempt to include the last 2 days of values for each ID1-ID2 pair. I would like to calculate moving average to handle NAs two separate ways: 1. Only calculate when there are 2 non-NA observations, otherwise avg should be NA (e.g. first 2 days within an ID1-ID2 will always have NAs)

Linear regression of time series over multiple columns

試著忘記壹切 提交于 2019-12-11 17:54:08
问题 I have the following problem. I want to compute the regression of an annual time series in matrix form. In total, I have 56 time series I extracted from gridpoints of an area I want to examine, so that I've got 56 values per year. I've plotted all values as points in a figure. Now I want to add a regression line to this figure, which contains all data. My goal is to compute the regression for the whole matrix. library(zoo) pdf(file="/home/user/name.pdf", pointsize=20, onefile = FALSE, width

How can I tell if a time point exists between a set of before and after times

谁都会走 提交于 2019-12-11 13:45:40
问题 I was trying to answer a question on stack overflow (Mapping multiple IDs using R) when I got stuck with how to finish it. Namely, how can I test if there is a time point between a set of before and after time points. The user from the post did not make a reproducible example but here is what I came up with. I want to test time points in hidenic_file$hidenic_time with the before and after times in dataframe emtek_file and return the emtek_id 's that match the time frame of each hidenic_id .

Reading CSV in R with zoo

别来无恙 提交于 2019-12-11 12:54:20
问题 I have a CSV in the following format: TICKER,PER,DATE,TIME,CLOSE SYMBOL,1,20160104,1002,14180.0000000 SYMBOL,1,20160104,1003,14241.0000000 I would like to read it into a time series: f <- function(a, b) { c <- paste(a, b) return(strptime(c, format = "%Y%m%d %H%M")) } d <- read.zoo("test.csv", FUN = f, index.column = list("DATE", "TIME")) And what I get is index does not match data . Why? 回答1: You need to specify header = TRUE and sep = "," , since they are not the defaults for read.zoo like

Fill in time series gaps with both LCOF and NOCB methods but acknowledge breaks in time series

孤者浪人 提交于 2019-12-11 12:50:04
问题 There are edits to this post at the end. I have a large dataset of daily dietary records for a population of individuals. There are data missing at random from each of the individuals. This is an example for one individual (I will eventually generalize this solution to the population): > str(final_daily) 'data.frame': 387 obs. of 10 variables: $ Date : chr "2014-08-13" "2014-08-14" "2014-08-15" "2014-08-16" ... $ MEID.1 : Factor w/ 97 levels "","1","1.1","1.1a",..: NA NA NA 17 24 NA NA NA NA

Merge or cbind xts object with dataframe

こ雲淡風輕ζ 提交于 2019-12-11 10:30:51
问题 I have a dataframe with monthly time series (various financial and economic variables), something like this: var1 <- c('1','2','3') var2 <- c('1','2','3') Date <- as.Date(c('1995-11-1','1995-12-1','1996-1-1')) df <- data.frame(Date, var1, var2) and I would like to add a number of further variables which I am downloading from FRED, like this: library('quantmod') y<-getSymbols('T10Y2Y',src='FRED', auto.assign=FALSE) y2<-to.monthly(y) y is an "xts" "zoo" object. Now, y2 is of a different length

expand zoo timeline in R

依然范特西╮ 提交于 2019-12-11 08:02:17
问题 Hi I have a time series dataset contains a few data points in Aug and Sep. How can I fill in the missing days with a default value easily, say 0 in this case: What I am thinking right now to merge the dataset with a sequential time series for the timeline I like, then do na.fill to replace NAs with the default value I want. This is what I have done: # This is my data z1 z1 <- zoo(c(1,2,3,4,5), as.Date(c('2013-08-09', '2013-08-12', '2013-09-02', '2013-09-09', '2013-09-15'))) # This is the