xts

Why apply() returns a transposed xts matrix?

时光毁灭记忆、已成空白 提交于 2019-11-26 21:02:48
I want to run a function on all periods of an xts matrix. apply() is very fast but the returned matrix has transposed dimensions compared to the original object: > dim(myxts) [1] 7429 48 > myxts.2 = apply(myxts, 1 , function(x) { return(x) }) > dim(myxts.2) [1] 48 7429 > str(myxts) An 'xts' object from 2012-01-03 09:30:00 to 2012-01-30 16:00:00 containing: Data: num [1:7429, 1:48] 4092500 4098500 4091500 4090300 4095200 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:48] "Open" "High" "Low" "Close" ... Indexed by objects of class: [POSIXlt,POSIXt] TZ: xts Attributes: NULL > str

What is an efficient method for partitioning and aggregating intervals from timestamped rows in a data frame?

我怕爱的太早我们不能终老 提交于 2019-11-26 16:38:06
问题 From a data frame with timestamped rows (strptime results), what is the best method for aggregating statistics for intervals? Intervals could be an hour, a day, etc. There's the aggregate function, but that doesn't help with assigning each row to an interval. I'm planning on adding a column to the data frame that denotes interval and using that with aggregate , but if there's a better solution it'd be great to hear it. Thanks for any pointers! Example Data Five rows with timestamps divided

Convert data frame with date column to timeseries

时光总嘲笑我的痴心妄想 提交于 2019-11-26 15:23:26
问题 I've got a data frame with the following data: >PRICE DATE CLOSE 1 20070103 54.700 2 20070104 54.770 3 20070105 55.120 4 20070108 54.870 5 20070109 54.860 6 20070110 54.270 7 20070111 54.770 8 20070112 55.360 9 20070115 55.760 ... As you can see my DATE column represents a date (yyyyMMdd) and my CLOSE column represents prices. I now have to calculate CalmarRatio, from the PerformanceAnalytics package. I'm new to R, so i can't understand everything, but from what i have googled to the moment i

Basic lag in R vector/dataframe

有些话、适合烂在心里 提交于 2019-11-26 15:09:01
Will most likely expose that I am new to R, but in SPSS, running lags is very easy. Obviously this is user error, but what I am missing? x <- sample(c(1:9), 10, replace = T) y <- lag(x, 1) ds <- cbind(x, y) ds Results in: x y [1,] 4 4 [2,] 6 6 [3,] 3 3 [4,] 4 4 [5,] 3 3 [6,] 5 5 [7,] 8 8 [8,] 9 9 [9,] 3 3 [10,] 7 7 I figured I would see: x y [1,] 4 [2,] 6 4 [3,] 3 6 [4,] 4 3 [5,] 3 4 [6,] 5 3 [7,] 8 5 [8,] 9 8 [9,] 3 9 [10,] 7 3 Any guidance will be much appreciated. Another way to deal with this is using the zoo package, which has a lag method that will pad the result with NA: require(zoo) >

Converting a data frame to xts

≯℡__Kan透↙ 提交于 2019-11-26 12:18:43
问题 I\'m trying to convert a data frame to xts object using the as.xts()-method. Here is my input dataframe q: q t x 1 2006-01-01 00:00:00 1 2 2006-01-01 01:00:00 2 3 2006-01-01 02:00:00 3 str(q) \'data.frame\': 10 obs. of 2 variables: $ t: POSIXct, format: \"2006-01-01 00:00:00\" \"2006-01-01 01:00:00\" \"2006-01-01 02:00:00\" \"2006-01-01 03:00:00\" ... $ x: int 1 2 3 4 5 6 7 8 9 10 The result is: > as.xts(q) Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard

Subset xts object by time of day

泄露秘密 提交于 2019-11-26 11:25:43
问题 A simple question: I know how to subset time series in xts for years, months and days from the help: x[\'2000-05/2001\'] and so on. But how can I subset my data by hours of the day? I would like to get all data between 07:00 am and 06:00 pm. I.e., I want to extract the data during business time - irrelevant of the day (I take care for weekends later on). Help has an example of the form: .parseISO8601(\'T08:30/T15:00\') But this does not work in my case. Does anybody have a clue? 回答1: If your

Return data subset time frames within another timeframes?

冷暖自知 提交于 2019-11-26 10:09:33
问题 There are very nifty ways of subsetting xts objects. For example, one can get all the data for all years, months, days but being strictly between 9:30 AM and 4 PM by doing: my_xts[\"T09:30/T16:00\"] Or you can get all the observations between two dates by doing: my_xts[\"2012-01-01/2012-03-31\"] Or all the dates before/after a certain date by doing: my_xts[\"/2011\"] # from start of data until end of 2011 my_xts[\"2011/\"] # from 2011 until the end of the data How can I get all the data for

Forecasting time series data

断了今生、忘了曾经 提交于 2019-11-26 08:03:50
问题 I\'ve done some research and I am stuck in finding the solution. I have a time series data, very basic data frame, let\'s call it x : Date Used 11/1/2011 587 11/2/2011 578 11/3/2011 600 11/4/2011 599 11/5/2011 678 11/6/2011 555 11/7/2011 650 11/8/2011 700 11/9/2011 600 11/10/2011 550 11/11/2011 600 11/12/2011 610 11/13/2011 590 11/14/2011 595 11/15/2011 601 11/16/2011 700 11/17/2011 650 11/18/2011 620 11/19/2011 645 11/20/2011 650 11/21/2011 639 11/22/2011 620 11/23/2011 600 11/24/2011 550 11

Why apply() returns a transposed xts matrix?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 07:48:57
问题 I want to run a function on all periods of an xts matrix. apply() is very fast but the returned matrix has transposed dimensions compared to the original object: > dim(myxts) [1] 7429 48 > myxts.2 = apply(myxts, 1 , function(x) { return(x) }) > dim(myxts.2) [1] 48 7429 > str(myxts) An \'xts\' object from 2012-01-03 09:30:00 to 2012-01-30 16:00:00 containing: Data: num [1:7429, 1:48] 4092500 4098500 4091500 4090300 4095200 ... - attr(*, \"dimnames\")=List of 2 ..$ : NULL ..$ : chr [1:48] \

Basic lag in R vector/dataframe

為{幸葍}努か 提交于 2019-11-26 04:11:57
问题 Will most likely expose that I am new to R, but in SPSS, running lags is very easy. Obviously this is user error, but what I am missing? x <- sample(c(1:9), 10, replace = T) y <- lag(x, 1) ds <- cbind(x, y) ds Results in: x y [1,] 4 4 [2,] 6 6 [3,] 3 3 [4,] 4 4 [5,] 3 3 [6,] 5 5 [7,] 8 8 [8,] 9 9 [9,] 3 3 [10,] 7 7 I figured I would see: x y [1,] 4 [2,] 6 4 [3,] 3 6 [4,] 4 3 [5,] 3 4 [6,] 5 3 [7,] 8 5 [8,] 9 8 [9,] 3 9 [10,] 7 3 Any guidance will be much appreciated. 回答1: Another way to deal