xts

Use endpoints function to get start points instead?

这一生的挚爱 提交于 2019-11-29 07:50:54
I have an xts object called Daily_Quotes that contains stock quotes. I'm using endpoints to get monthly stock quotes that I retrieved using getSymbols (from the quantmod package). I noticed that the endpoints function creates an index of the row that contains the last trading day for the particular month and assigns it to the new object for the specified date range. Is there anyway to get first trading day of the month instead? # My code Monthly_Quotes <- Daily_Quotes[endpoints(Daily_Quotes,'months')] What I tried doing was: # This gave me the next day or 1st day of the next month # or next

Converting an XTS object to a data.frame [duplicate]

纵然是瞬间 提交于 2019-11-29 07:37:52
Possible Duplicate: How to create a variable of rownames? Please run it in R: require(quantmod) setSymbolLookup(SDB=list(name="000001.sz",src="yahoo")) getSymbols("SDB",from="2010-01-01",to="2010-02-01") sdb=as.data.frame(weeklyReturn(SDB)) sdb What I get is: weekly.returns 2010-01-08 -0.07830343 2010-01-15 -0.05176991 2010-01-22 0.07699487 2010-01-29 -0.05979203 2010-02-01 -0.02119816 What I want to get is: date weekly.returns 1 2010-01-08 -0.07830343 2 2010-01-15 -0.05176991 3 2010-01-22 0.07699487 4 2010-01-29 -0.05979203 5 2010-02-01 -0.02119816 How can I do this? Note that this is an XTS

Why is there no apply.hourly in R with xts/zoo?

落爺英雄遲暮 提交于 2019-11-29 05:06:23
问题 I want to aggregate data by hourly mean. Daily is very easy: apply.daily(X2,mean) Why is there no function for hourly? I tried hr.means <- aggregate(X2, format(X2["timestamp"],"%Y-%m-%d %H")) and got always error with trim argument. Is there an easier function similar to apply.daily? What if I want to aggregate the mean of 5 minutes. Data are values per minute: "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

can I write an xts object using write.csv in R

六眼飞鱼酱① 提交于 2019-11-29 04:02:57
I have an xts object, the first column of which is date-time, followed by OHLC. when I type >test it prints the correct output as follows: 2010-09-08 15:13:00 115 115 110 115 2010-09-08 15:14:00 120 125 115 125 however, when I try write.csv(test,"test.csv") it only writes the OHLC - why. what command do I use to also write the date-time this is what str(test) looks like: An ‘xts’ object from 2010-06-30 15:47:00 to 2010-09-08 15:14:00 containing: Data: num [1:21757, 1:4] 215 220 205 195 185 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:4] "y.Open" "y.High" "y.Low" "y.Close"

R: adding 1 month to a date

独自空忆成欢 提交于 2019-11-29 04:01:26
I want to get the date sequence between a startDate and endDate by adding 1 month to the startDate . ie, if startDate is 2013-01-31 and endDate is 2013-07-31, I would prefer to see dates like this: "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30" "2013-05-31" "2013-06-30" "2013-07-31" I have tried seq.Date(as.Date("2013-01-31"),by="month",length.out=7) . But the output of this code is like this > seq.Date(as.Date("2013-01-31"),by="month",length.out=7) [1] "2013-01-31" "2013-03-03" "2013-03-31" "2013-05-01" "2013-05-31" "2013-07-01" "2013-07-31" So,what is the simplest solution to get the

Add missing xts/zoo data with linear interpolation in R

寵の児 提交于 2019-11-29 02:22:19
I do have problems with missing data, but I do not have NAs - otherwise would be easier to handle... My data looks like this: time, value 2012-11-30 10:28:00, 12.9 2012-11-30 10:29:00, 5.5 2012-11-30 10:30:00, 5.5 2012-11-30 10:31:00, 5.5 2012-11-30 10:32:00, 9 2012-11-30 10:35:00, 9 2012-11-30 10:36:00, 14.4 2012-11-30 10:38:00, 12.6 As you can see - there are missing some minute values - it is xts/zoo so I use as.POSIXct... to set the date as an index. How to add the missing timesteps to get a full ts? I want to fill the missing values with linear interpolation. Thank you for your help! You

Convert daily to weekly/monthly data with R

天涯浪子 提交于 2019-11-29 00:10:49
I have daily prices series over a wide range of products; I want to convert to a new dataframe with weekly or monthly data. I first used xts in order to apply the to.weekly function...which works only for OHLC format. I am sure there may exist a function similar to to.weekly but for dataframe where the format is not OHLC. There a different posts already related to this as the following: Does rollapply() allow an array of results from call to function? or Averaging daily data into weekly data I am eventually using: length(bra) 1 2416 test<-bra[seq(1,2416,7),] Would there be a more efficient

R xts and data.table

北战南征 提交于 2019-11-28 23:05:01
问题 I can convert a data.table to an xts object just as I do with a data.frame: > df = data.frame(x = c("a", "b", "c", "d"), v = rnorm(4)) > dt = data.table(x = c("a", "b", "c", "d"), v = rnorm(4)) > xts(df, as.POSIXlt(c("2011-01-01 15:30:00", "2011-01-02 15:30:00", "2011-01-03 15:50:50", "2011-01-04 15:30:00"))) x v 2011-01-01 15:30:00 "a" "-1.2232283" 2011-01-02 15:30:00 "b" "-0.1654551" 2011-01-03 15:50:50 "c" "-0.4456202" 2011-01-04 15:30:00 "d" "-0.9416562" > xts(dt, as.POSIXlt(c("2011-01-01

rowSums but keeping NA values

和自甴很熟 提交于 2019-11-28 13:41:10
I am pretty sure this is quite simple, but seem to have got stuck...I have two xts vectors that have been merged together, which contain numeric values and NAs. I would like to get the rowSums for each index period, but keeping the NA values. Below is a reproducible example set.seed(120) dd <- xts(rnorm(100),Sys.Date()-c(100:1)) dd1 <- ifelse(dd<(-0.5),dd*-1,NA) dd2 <- ifelse((dd^2)>0.5,dd,NA) mm <- merge(dd1,dd2) mm$m <- rowSums(mm,na.rm=TRUE) tail(mm,10) dd1 dd2 m 2013-08-02 NA NA 0.000000 2013-08-03 NA NA 0.000000 2013-08-04 NA NA 0.000000 2013-08-05 1.2542692 -1.2542692 0.000000 2013-08-06

R xts: .001 millisecond in index

巧了我就是萌 提交于 2019-11-28 12:58:33
It looks like POSIXlt allows millisecond precision specification, but I have a problem when setting a .001 millisecond index value in an xts object: > options(digits.secs = 3) > data(sample_matrix) > sample.xts = xts(sample_matrix, rep(as.POSIXlt("2012-03-20 09:02:50.001"), 180)) > head(sample.xts, 10) Open High Low Close 2012-03-20 09:02:50.000 50.03978 50.11778 49.95041 50.11778 2012-03-20 09:02:50.000 50.23050 50.42188 50.23050 50.39767 2012-03-20 09:02:50.000 50.42096 50.42096 50.26414 50.33236 2012-03-20 09:02:50.000 50.37347 50.37347 50.22103 50.33459 2012-03-20 09:02:50.000 50.24433 50