zoo

Convert Excel numeric to date

我的未来我决定 提交于 2019-12-10 10:38:18
问题 I have a vector of numeric excel dates i.e. date <- c(42963,42994,42903,42933,42964) The output am I expecting when using excel_to_numeric_date function from janitor package and as.yearmon function from zoo package as.yearmon(excel_numeric_to_date(date)) [1] "Aug 2016" "Sep 2016" "Jun 2017" "Jul 2017" "Aug 2017" . However, the conversion for the first to elements of the date vector are incorrect. The actual result are: as.yearmon(excel_numeric_to_date(date)) [1] "Aug 2017" "Sep 2017" "Jun

rollapply time series in R (zoo) on backward looking data

只谈情不闲聊 提交于 2019-12-10 07:04:20
问题 I would like to use the zoo function rollapply to apply a function (for example mean) on a time series but only using the last N known points. For example: x = zoo(c(1,2,3,4), order.by=c(10,11,12,13)) rollmean(x,2) Produces: 10 11 12 1.5 2.5 3.5 I would like to produce a series that would have date entries of 11, 12, 13 and values of 1.5, 2.5, 3.5. The values seem correct but the dates that rollmean outputs don't seem to correspond to what I would like. I'm a bit worried about just assigning

Write xts/zoo object to csv with index

微笑、不失礼 提交于 2019-12-10 02:41:54
问题 > library(PerformanceAnalytics) > data(managers) > class(managers) [1] "xts" "zoo" > head(managers) HAM1 HAM2 HAM3 HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR US 10Y TR US 3m TR 1996-01-31 0.0074 NA 0.0349 0.0222 NA NA NA 0.0340 0.00380 0.00456 1996-02-29 0.0193 NA 0.0351 0.0195 NA NA NA 0.0093 -0.03532 0.00398 1996-03-31 0.0155 NA 0.0258 -0.0098 NA NA NA 0.0096 -0.01057 0.00371 1996-04-30 -0.0091 NA 0.0449 0.0236 NA NA NA 0.0147 -0.01739 0.00428 1996-05-31 0.0076 NA 0.0353 0.0028 NA NA NA 0.0258 -0

Creating daily OHLC with custom starting time

本秂侑毒 提交于 2019-12-09 19:57:46
问题 I have 15-minute OHLC data and want to convert to daily OHLC but with the start of the day at 17:00:00. This way, the resulting daily bar should span from 17:00:00 to 17:00:00, not from 00:00:00 to 00:00:00 _ x <- zoo(runif(25), order.by=seq( as.POSIXct("2010-05-03 17:00:00"), as.POSIXct("2010-05-06 17:00:00"), by="15 min" ) ) _ head(x) 2010-05-03 17:00:00 0.9788685 2010-05-03 17:15:00 0.5414294 2010-05-03 17:30:00 0.8435366 2010-05-03 17:45:00 0.3064713 2010-05-03 18:00:00 0.1395849 2010-05

Most efficient/vectorization when using previous calculated value (rolling)

ぃ、小莉子 提交于 2019-12-09 06:12:38
问题 Following those conversations: Can I vectorize a calculation which depends on previous elements sapply? tapply? ddply? dataframe variable based on rolling index of previous values of another variable I wanted to test a more "real-life" case-study. I recently had to migrate SAS code to R and kdb code to R code. I tried to compiled a simple enough yet more sophisticated example to optimize. let's build the training set buildDF <- function(N){ set.seed(123); dateTimes <- sort(as.POSIXct("2001-01

Trouble finding non-unique index entries in zooreg time series

跟風遠走 提交于 2019-12-09 03:23:48
问题 I have several years of data that I'm trying to work into a zoo object (.csv at Dropbox). I'm given an error once the data is coerced into a zoo object. I cannot find any duplicated in the index. df <- read.csv(choose.files(default = "", caption = "Select data source", multi = FALSE), na.strings="*") df <- read.zoo(df, format = "%Y/%m/%d %H:%M", regular = TRUE, row.names = FALSE, col.names = TRUE, index.column = 1) Warning message: In zoo(rval3, ix) : some methods for “zoo” objects do not

How to create time series with missing datetime values

若如初见. 提交于 2019-12-08 10:12:13
问题 I have csv file with data. Link is here. Granularity of time series is 5 min for year 2013. However, values are missing for some time stamps. I want to create a time series with 5 minute interval with value zero for time stamps which are missing. Please advise how to do this either in Pandas or R 回答1: This should work # partial old data used for example timedata<- read.table(header = TRUE, sep =",", text = " timestamp, value 01/01/2013 00:00:10,10 01/01/2013 00:00:25,6 01/01/2013 00:00:40,10

Lags in R within specific subsets?

丶灬走出姿态 提交于 2019-12-08 08:12:08
问题 Suppose I have the following dataframe: df <- data.frame("yearmonth"=c("2005-01","2005-02","2005-03","2005-01","2005-02","2005-03"),"state"=c(1,1,1,2,2,2),"county"=c(3,3,3,3,3,3),"unemp"=c(4.0,3.6,1.4,3.7,6.5,5.4)) I'm trying to create a lag for unemployment within each unique state-county combination. I want to end up with this: df2 <- data.frame("yearmonth"=c("2005-01","2005-02","2005-03","2005-01","2005-02","2005-03"),"state"=c(1,1,1,2,2,2),"county"=c(3,3,3,3,3,3),"unemp"=c(4.0,3.6,1.4,3.7

Merging data - Error in fix.by(by.x, x)

微笑、不失礼 提交于 2019-12-08 07:42:38
问题 I am trying to merge data in R as suggested in an answer to my other post here. Yet, I have an error. First let me explain what I try to do. I have 100 files (each have x_i and y_i), I want to merge them in this way: from: x1; y1 ; x2 ; y2 1 ; 100 ; 1 ; 150 4 ; 90 ; 2 ; 85 7 ; 85 ; 10 ; 60 10; 80 ; to x1; y1 ; x2 ; y2 1 ; 100 ; 1 ; 150 2 ; 100 ; 2 ; 85 4 ; 90 ; 4 ; 85 7 ; 85 ; 7 ; 85 10; 80 ;10 ; 60 The simple script works fine on the toy example: xx <- read.table(text='x1; y1 ; x2 ; y2 1 ;

Error in read.zoo index has bad entry at data row 2

丶灬走出姿态 提交于 2019-12-08 04:31:17
问题 When I use the following read.zoo it goes great until I added the last line: (my source is CSV, but here it is in a format for reproducing): library(zoo) Lines <- "fdatetime,Consumption 1,27/03/2015 01:00,0.04 2,27/03/2015 02:00,0.04" > z <- read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",") Error in read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",") : index has bad entry at data row 51 What's wrong with the last line? If you delete the last line it will