0

I have a vector of dates that I would like to converte into class "POSIXct" "POSIXt" with specific time zones specified. Within this vector, dates can relate to one of three different time zones; Australia/Sydney, Australia/Adelaide or Australia/Perth.

I can convert dates to a single time zone and the correct "POSIXct" "POSIXt" class using as.POSIXct() without any problem, see below. This works for all three of my desired time zones. However, when I simultaneously try to convert these dates to their respective three different times zones within an ifelse() statement the resulting output is a vector of class numeric.

Q: What am I doing wrong? Why can I achieve the correct "POSIXct" "POSIXt" class for the vector of dates outside of the ifelse() statement but not from within the ifelse() statement?

My final desired output is a vector of dates of class "POSIXct" "POSIXt" where some dates will relate to time zone Australia/Sydney, some will relate to time zone Australia/Adelaide and some will relate to time zone Australia/Perth.

# Example of original data 
head(dat$date.posixct)
[1] "24/04/2015 00:00" "25/04/2015 00:00" "26/04/2015 00:00" "14/07/2015 00:00" "15/07/2015 00:00" "16/07/2015 00:00"

# Length of origional data
length(dat$date.posixct)
[1] 528

# Calling the ```as.POSIXct()``` function directly returns the correct and desired date format for tz = "Australia/Sydney"
as.POSIXct(dat$date.posixct, format = "%d/%m/%Y %H:%M", tz = "Australia/Sydney")
  [1] "2015-04-24 AEST" "2015-04-25 AEST" "2015-04-26 AEST" "2015-07-14 AEST" "2015-07-15 AEST" "2015-07-16 AEST" "2015-10-13 AEDT"
.... I have deleted many rows to save text

# Calling the ```as.POSIXct()``` function directly returns the correct and desired date format for tz = "Australia/Adelaide"
as.POSIXct(dat$date.posixct, "%d/%m/%Y %H:%M", tz = "Australia/Adelaide")
  [1] "2015-04-24 ACST" "2015-04-25 ACST" "2015-04-26 ACST" "2015-07-14 ACST" "2015-07-15 ACST" "2015-07-16 ACST" "2015-10-13 ACDT"
.......I have deleted many rows to save text

# Calling the ```as.POSIXct()``` function directly returns the correct and desired date format for tz = "Australia/Perth"
as.POSIXct(dat$date.posixct, "%d/%m/%Y %H:%M", tz = "Australia/Perth")
  [1] "2015-04-24 AWST" "2015-04-25 AWST" "2015-04-26 AWST" "2015-07-14 AWST" "2015-07-15 AWST" "2015-07-16 AWST" "2015-10-13 AWST"
..... I have deleted many rows to save text

# Calling the ```as.POSIXct()``` function within the ```ifelse()``` function returns the incorrect and undesired numeric date format 
ifelse(dat$time.zone == "AEST", as.POSIXct(dat$date.posixct, format = "%d/%m/%Y %H:%M", tz = "Australia/Sydney"), ifelse(dat$time.zone == "ACST", as.POSIXct(dat$date.posixct, "%d/%m/%Y %H:%M", tz = "Australia/Adelaide"), as.POSIXct(dat$date.posixct, "%d/%m/%Y %H:%M", tz = "Australia/Perth")))
  
[1] 1429797600 1429884000 1429970400 1436796000 1436882400 1436968800 1444654800 1444741200 1444827600 1452517200 1452603600 1452690000
.......I have deleted many rows to reduce text 
Pat Taggart
  • 321
  • 1
  • 9
  • 2
    You cannot have different timezones in the same vector. A vector can have only one timezone. – Ronak Shah Mar 01 '21 at 06:30
  • 2
    Does this answer your question? [How to prevent ifelse() from turning Date objects into numeric objects](https://stackoverflow.com/questions/6668963/how-to-prevent-ifelse-from-turning-date-objects-into-numeric-objects) – Waldi Mar 01 '21 at 07:01

0 Answers0