简体   繁体   中英

Why does as.Date applied to an xts index produce a different result?

Why do x and y (below) produce different results when filtering the xts object? Both x and y appear to store unique dates, one as characters and the other as dates. ob[x] returns all records. ob[y] returns 1 record per date (only if a record matches to midnight, 00:00:00 ).

seq1<- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-14"), by = "30 mins")
ob<- xts(data.frame(closingPrice=1:(length(seq1))),seq1)
x = unique(format(index(ob), format = "%Y-%m-%d"))
y = as.Date(unique(format(index(ob), format = y = as.Date(unique(format(index(ob), format = "%Y-%m-%d"))))))
ob[x]
ob[y]

x is a character vector, y is a Date vector. When you subset an xts object by a date-time object, you only get exact matches (in this case, midnight of each day).

When you subset by a character vector, you use xts' ISO-8601-based subsetting (see ?"[.xts" ). One feature of that type of subsetting is that you get all observations that match up to the lowest specified component

You specified year, month, and day, so you'll get all index observations that occur on that specific day. For another example: specify everything up to an hour, and you'll get all observations for that hour.

> ob[paste(x[1],"12")]
                    closingPrice
2015-09-01 12:00:00           25
2015-09-01 12:30:00           26

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM