Coercing a POSIXct object to Date object

不打扰是莪最后的温柔 提交于 2019-12-12 11:18:43

问题


Reproducible code:

# Loading quantmod

library(quantmod)

# Please, put in R this structure

a <- structure(c(2.4, 2.35, 2.44, 2.44, 2.31, 2.32, 2.41, 2.43, 2.46, 
            2.42, 2.45, 2.39, 2.3, 2.41, 2.33, 2.37, 2.38, 2.4, 2.275, 2.235, 
            -0.0300000000000002, -0.0499999999999998, 0.0899999999999999, 
            0, -0.13, 0.00999999999999979, 0.0900000000000003, 0.02, 0.0299999999999998, 
            -0.04, 0.0300000000000002, -0.0600000000000001, -0.0900000000000003, 
            0.11, -0.0800000000000001, 0.04, 0.00999999999999979, 0.02, -0.125, 
            -0.04, 2.75684036527164, 2.80336038090653, 2.85070650150373, 
            2.80638610182307, 2.78192004966867, 2.75238601492226, 2.72915916431245, 
            2.7324175575505, 2.77446196662146, 2.78562833574758, 2.90799335924598, 
            2.96217549002515, 2.93863268151342, 2.86048512414597, 2.89259151463448, 
            2.94864066602014, 2.88926002904347, 2.86619290219901, 2.83438912314523, 
            2.7868613815265, -0.0410505398303571, 0.0465200156348926, 0.0473461205971986, 
            -0.0443203996806614, -0.0244660521544064, -0.0295340347464039, 
            -0.0232268506098166, 0.00325839323805921, 0.0420444090709573, 
            0.0111663691261232, 0.1223650234984, 0.0541821307791639, -0.02354280851173, 
            -0.078147557367453, 0.0321063904885124, 0.0560491513856629, -0.0593806369766661, 
            -0.0230671268444684, -0.031803779053778, -0.0475277416187283, 
            0.172217775014356, 0.147897402545627, 0.107842980922542, 0.111384630261207, 
            0.116199395018325, 0.119482271409344, 0.130753386099011, 0.119444345583785, 
            0.101923549299963, 0.1002890717992, 0.032754605975247, 0.00669932935895989, 
            0.0105319562242379, 0.0453224418800389, 0.0294942187512146, -0.0142520227072014, 
            0.0285107030408045, 0.0335789802090738, 0.0520861644718136, 0.0759107647712609, 
            0.0100035842055157, -0.0243203724687295, -0.0400544216230849, 
            0.0035416493386653, 0.00481476475711817, 0.00328287639101843, 
            0.0112711146896673, -0.0113090405152261, -0.0175207962838217, 
            -0.00163447750076351, -0.0675344658239525, -0.0260552766162871, 
            0.00383262686527797, 0.034790485655801, -0.0158282231288243, 
            -0.043746241458416, 0.0427627257480059, 0.0050682771682693, 0.0185071842627398, 
            0.0238246002994473, -0.0191994306395253, -0.0205181532675806, 
            -0.0249013268238319, -0.0237728532488024, 0.023560859824188, 
            0.0493604748650355, -0.0226612828685699, -0.0242381517516994, 
            -0.0229683470958784, 0.0193184371423425, -0.0146720746595668, 
            -0.0103171513510591, -0.0153945062856312, -0.0207074718973363, 
            0.113843134319118, -0.017036109656239, 0.00401445822575002, -0.0152979313119177, 
            -0.00168455008690644, 0.0234396377388245), .indexTZ = "", .indexCLASS = c("POSIXct", 
                                                                                      "POSIXt"), tclass = c("POSIXct", "POSIXt"), tzone = "", na.action = structure(1L, class = "omit", index = 1090364400), class = c("xts", 
                                                                                                                                                                                                                       "zoo"), index = structure(c(1090450800, 1090537200, 1090796400, 
                                                                                                                                                                                                                                                   1090882800, 1090969200, 1091055600, 1091142000, 1091401200, 1091487600, 
                                                                                                                                                                                                                                                   1091574000, 1091660400, 1091746800, 1092006000, 1092092400, 1092178800, 
                                                                                                                                                                                                                                                   1092265200, 1092351600, 1092610800, 1092697200, 1092783600), tzone = "", tclass = c("POSIXct", 
                                                                                                                                                                                                                                                                                                                                       "POSIXt")), .Dim = c(20L, 7L), .Dimnames = list(NULL, c("Inflation", 
                                                                                                                                                                                                                                                                                                                                                                                               "dInflation", "Fear", "dFear", "Greed", "dGreed", "Growth")))
# These are 'a' dates

index(a)

# You can see it is a POISXct object whose last element is equal to 
# "2004-08-18 01:00:00 CEST"

is(index(a))

# Coercing to Date object

as.Date(index(a))

# Last element goes back by 1 day to "2004-08-17"

From the code you can see that each date is brought back by 1 day when I try to coerce that POSIXct object to a Date one.

I would like to preserve the original date when I run this code: what should I modify in my code?

These are my session informations:

R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] quantmod_0.4-0             TTR_0.21-1                 Defaults_1.1-1            
 [4] PerformanceAnalytics_1.1.0 Matrix_1.0-11              lattice_0.20-13           
 [7] MASS_7.3-23                Rsolnp_1.14                truncnorm_1.0-6           
[10] numDeriv_2012.9-1          xts_0.9-3                  zoo_1.7-9                 
[13] RcppArmadillo_0.3.6.3      Rcpp_0.10.2                rcom_2.2-5                
[16] rscproxy_2.0-5            

loaded via a namespace (and not attached):
[1] grid_2.15.2        rmgarch_1.00-4     rugarch_1.01-5     timeDate_2160.97  
[5] timeSeries_2160.95 tools_2.15.2      

回答1:


I think you just need to add your timezone to the conversion. Have you tried...

as.Date(index(a) , tz = "CET")
 [1] "2004-07-22" "2004-07-23" "2004-07-26" "2004-07-27" "2004-07-28"
 [6] "2004-07-29" "2004-07-30" "2004-08-02" "2004-08-03" "2004-08-04"
 [11] "2004-08-05" "2004-08-06" "2004-08-09" "2004-08-10" "2004-08-11"
 [16] "2004-08-12" "2004-08-13" "2004-08-16" "2004-08-17" "2004-08-18"

index(a)
[1] "2004-07-22 BST" "2004-07-23 BST" "2004-07-26 BST" "2004-07-27 BST"
[5] "2004-07-28 BST" "2004-07-29 BST" "2004-07-30 BST" "2004-08-02 BST"
[9] "2004-08-03 BST" "2004-08-04 BST" "2004-08-05 BST" "2004-08-06 BST"
[13] "2004-08-09 BST" "2004-08-10 BST" "2004-08-11 BST" "2004-08-12 BST"
[17] "2004-08-13 BST" "2004-08-16 BST" "2004-08-17 BST" "2004-08-18 BST"



回答2:


This is a timezone issue. You need to set the timezone of the index and/or the system TZ environment variable.

Sys.setenv(TZ="Europe/Berlin")
indexTZ(a) <- "Europe/Berlin"
as.Date(index(a))

It's good practice to set the system TZ environment variable when dealing with date/time objects.



来源:https://stackoverflow.com/questions/15640133/coercing-a-posixct-object-to-date-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!