I would like to get the week and day of an xts object. I tried to extract the index value of my data set (based on SPY data using quantmod
package) without success. How could I do this?
require(quantmod)
options(scipen=999)
spy<-getSymbols(("SPY") , src = 'yahoo', from = '2007-01-01', auto.assign = T)
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2016-01-22 189.78 190.76 188.88 190.52 163849600 190.52
2016-01-25 189.92 190.15 187.41 187.64 122676200 187.64
2016-01-26 188.42 190.53 188.02 190.20 137269900 190.20
2016-01-27 189.58 191.56 187.06 188.13 181677100 188.13
2016-01-28 189.96 190.20 187.16 189.11 139970600 189.11
2016-01-29 190.02 193.88 189.88 193.72 195455400 193.72
SPY$Date<-as.Date(index(SPY))
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume Date
2016-01-22 189.78 190.76 188.88 190.52 163849600 190.52
2016-01-25 189.92 190.15 187.41 187.64 122676200 187.64
2016-01-26 188.42 190.53 188.02 190.20 137269900 190.20
2016-01-27 189.58 191.56 187.06 188.13 181677100 188.13
2016-01-28 189.96 190.20 187.16 189.11 139970600 189.11
2016-01-29 190.02 193.88 189.88 193.72 195455400 193.72
SPY$Date<-as.character(index(SPY))
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume Date
2016-01-22 "189.779999" "190.759995" "188.880005" "190.520004" "163849600" "2016-01-22"
2016-01-25 "189.919998" "190.149994" "187.410004" "187.639999" "122676200" "2016-01-25"
2016-01-26 "188.419998" "190.529999" "188.020004" "190.199997" "137269900" "2016-01-26"
2016-01-27 "189.580002" "191.559998" "187.059998" "188.130005" "181677100" "2016-01-27"
2016-01-28 "189.960007" "190.199997" "187.160004" "189.110001" "139970600" "2016-01-28"
2016-01-29 "190.020004" "193.880005" "189.880005" "193.720001" "195455400" "2016-01-29"
You could use .indexwday
to get the weekday of the index. It returns the wday
vector from a POSIXlt
representation of the index, so see the Details section of ?POSIXlt
for a description.
require(quantmod)
getSymbols("SPY")
SPY$weekday <- .indexwday(SPY)