我需要在动物园对象的索引上使用 as.Date 。一些日期在 BST 中,因此在转换时我(仅)在这些条目上失去了一天。我根本不在乎一小时的差异甚至日期的时间部分,我只想确保显示的日期保持不变。我猜这不是很难,但我无法管理它。有人可以帮忙吗?
class(xtsRet)
#[1] "xts" "zoo"
index(xtsRet)
#[1] "2007-07-31 BST" "2007-08-31 BST" "2007-09-30 BST" "2007-10-31 GMT"
class(index(xtsRet))
#[1] "POSIXt" "POSIXct"
index(xtsRet) <- as.Date(index(xtsRet))
index(xtsRet)
#[1] "2007-07-30" "2007-08-30" "2007-09-29" "2007-10-31"
最小可复制示例(不需要zoo
包):
my_date <- as.POSIXct("2007-04-01") # Users in non-UK timezone will need to
# do as.POSIXct("2007-04-01", "Europe/London")
my_date
#[1] "2017-04-01 BST"
as.Date(my_date)
#[1] "2017-03-31"