简体   繁体   中英

initialized POSIXct value different from printed values in R

Below, I write a small code to compare the results of the output of POSIXct and the print value. Does anyone know why the values in the loop are different from printed values or x variable?

    timezones <- data.frame(dst_start= '2012-03-11 3:00', TZname='America/Vancouver')
    timezones$TZname <- as.character(timezones$TZname)

    # Initialize the column 
    timezones$ET_DST_start <- .POSIXct(1)

    timezones$ET_DST_start[1] <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
    # Assigned value to dataframe
    timezones$ET_DST_start[1]
    # 
    as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
    # assigned value to a non_initialized variable
    x <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
    x

Why does ET_DST_start value differ from x or when I just directly convert it? I think it has to do with initializing the column, but I don't know why this happens. Any thoughts?

It has indeed to do with initializing the column. The reason is that a POSIXct object comes with a time zone attribute. It is not an attribute of a separate element of the vector, but rather a property of the entire vector. When you initialise the column, that attribute is set and if you later change a single element of the column, the date is converted to match that time zone.

You can see that the time zone that you use initially matters as follows:

# use UTC
timezones$ET_DST_start <- .POSIXct(1, tz = "UTC")
timezones$ET_DST_start[1] <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
timezones$ET_DST_start[1]
## [1] "2012-03-11 10:00:00 UTC"
# use CET
timezones$ET_DST_start <- .POSIXct(1, tz = "CET")
timezones$ET_DST_start[1] <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
timezones$ET_DST_start[1]
## [1] "2012-03-11 11:00:00 CET"

The output differs and you can also see that the time zone you chose initially is kept. If you overwrite the entire column and not just one element of it, the time zone is newly set:

# use UTC
timezones$ET_DST_start <- .POSIXct(1, tz = "UTC")
timezones$ET_DST_start <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
timezones$ET_DST_start
## [1] "2012-03-11 03:00:00 PDT"
# use CET
timezones$ET_DST_start <- .POSIXct(1, tz = "CET")
timezones$ET_DST_start <- as.POSIXct(timezones$dst_start[1], tz=timezones$TZname[1])
timezones$ET_DST_start
## [1] "2012-03-11 03:00:00 PDT"

Here, the final result does not depend on the initial choice of time zone.

The final lines of your example code correspond to the same situation: You do not assign the time stamp to another vector that has already a time zone preconfigured, so there is no conversion taking place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM