Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as.POSIXlt ignores tz argument

Tags:

r

Pretty basic, just not understanding what the tz argument is for if not for this...

dateIWantToConvert <- as.Date("2013-06-01") #the format of my starting object

as.POSIXlt(dateIWantToConvert, tz="America/Chicago")
[1] "2013-06-01 UTC"

There are a whole lot of these in a data.frame, so efficiency is important.

like image 659
hedgedandlevered Avatar asked Oct 26 '25 13:10

hedgedandlevered


2 Answers

A more reliable way to do this is to use strptime. This takes any tz arguement from OlsonNames(). And by reliable, I mean that this should be OS independent.

(vec.Date <- strptime("2013-06-01", format = "%Y-%m-%d", tz = "America/Chicago"))
# "2013-06-01 CDT"
class(vec.Date)
# [1] "POSIXlt" "POSIXt" 

And for your question regarding efficiency, you really should be using strptime. See Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt

like image 58
R J Avatar answered Oct 29 '25 03:10

R J


You could try without as.Date():

> as.POSIXlt("2013-06-01", tz="America/Chicago")
#[1] "2013-06-01 CDT"
like image 45
RHertel Avatar answered Oct 29 '25 02:10

RHertel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!