Reputation: 11
I'm trying to convert a string to a POSIXct object, but if I do this
x <- as.POSIXct('15/10/17', tz = 'America/Sao_Paulo', format = '%d/%m/%y')
the output is NA. I think that it occurs because it became daylight saving time in São Paulo on day 15/10/17, so midnight of that day doesn't exist. One solution is to force x to be another value in that day like
x <- structure(1508036400, class = c("POSIXct", "POSIXt"), tzone = "America/Sao_Paulo")
But I was wodering if there is a more elegant solution.
My session info:
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C LC_TIME=Portuguese_Brazil.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.2 tools_3.4.2 yaml_2.1.14 fortunes_1.5-4
Thanks in advance!
Upvotes: 0
Views: 56
Reputation: 121077
It's not clear if this is a bug in as.POSIXct()
or if it is saving you from an obscure data problem.
In either case, if you only have date information, then it is best practice to store your data as a Date
rather than POSIXct
.
Try as.Date('15/10/17', format = '%d/%m/%y')
.
Upvotes: 1