2016-10-07 30 views
1

我創建了下面的設置。最終結果是xts,其中索引 的格式爲as.POSIXct,包括日期和時間。xts無法搜索沒有時間戳的日期,當使用POSIXct

我的問題是如何在索引中搜索不包含搜索時間戳的日期, ?是否有另一種方法可以跳躍搜索獨家日期而不必輸入時間?

這裏是我的代碼:

# create a set of test data to be used 
d <- as.POSIXct(c("2014/10/06 06:30:00", "2015/02/02 12:27:00", 
        "2015/02/05 21:27:00", "2016/06/12 14:21:00")) 
o = c(98.00, 97.67, 98.00, 98.10) # for "open" data 
h = c(99.71, 98.97, 99.71, 99.41) # for "high" data 
l = c(96.81, 96.86, 96.81, 97.70) # for "low" data 
c = c(97.67, 98.67, 97.67, 98.83) # for "close" data 
v = c(1000, 22000, 1000, 50000) # for "volume" data 
a = c(1000, 22000, 1000, 50000) # for "adjusted" data 

# create a dataframe 
mydf1 <- data.frame("date" = d, "open" = o, "high" = h, "low" = l, 
"close" = c, "volume" = v, "adjusted" = a) 

# create an xts based on dataframe mydf1 
myxts1 <- xts(mydf1[,-1], order.by = mydf1$date) 

該命令不返回任何結果。

myxts1[as.POSIXct("2014-10-06")] 

...同時所有這些命令的工作表示該XTS指數 返回結果。

first(myxts1, "year") 
last(myxts1, "year") 
myxts1[as.POSIXct("2014-10-06 06:30:00")] 
window(myxts1, start = as.POSIXct("2015-02-02 12:27:00"), 
     end = as.POSIXct("2015-02-05 21:27:00")) 
+0

您想使用'myxts1 [「2014-10-06」]'。如果你提供一個Posixct時間xts試圖做一個完全匹配,在這種情況下它找不到。 – user3293236

回答

1

你想如果你提供一個POSIXct對象使用

myxts1["2014-10-06"] 

,XTS試圖使精確匹配,它不會在這種情況下找到。

相關問題