Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I just want to know that can we stop the aggregate converting datetimes to the computer's local timezone? Say, for instance:

dtUTC <- as.POSIXct(c('2010-01-01 01:01:01', '2015-01-02 07:23:11',

                      '2016-06-02 05:23:41', '2018-01-08 17:57:43'), tz='UTC')

groups <- c(1,1,2,2)

result <- aggregate(dtUTC, by=list(groups), FUN=min)

The result is converted to my computer's local timezone.

> dtUTC

[1] "2010-01-01 01:01:01 UTC" "2015-01-02 07:23:11 UTC" "2016-06-02 05:23:41 UTC"

[4] "2018-01-08 17:57:43 UTC"

> result$x

[1] "2010-01-01 12:01:01 AEDT" "2016-06-02 15:23:41 AEST"

I can convert it back post hoc but I am having multiple datetime columns, how can I do that?

attr(result$x, 'tzone') <- 'UTC'

> result$x

[1] "2010-01-01 01:01:01 UTC" "2016-06-02 05:23:41 UTC"

1 Answer

0 votes
by (108k points)

For multiple columns, you can simply use the dplyr library to aggregate:

library(lubridate)

library(dplyr)

dtUTC <- as.POSIXct(c('2010-01-01 01:01:01', '2015-01-02 07:23:11',

                      '2016-06-02 05:23:41', '2018-01-08 17:57:43'), tz='UTC')

groups <- c(1,1,2,2)

b<-data.frame(date= dtUTC, group = groups) %>% group_by(group) %>% dplyr::summarise(min = min(date))

b$min

> b$min

[1] "2010-01-01 01:01:01 UTC" "2016-06-02 05:23:41 UTC"

If you are a beginner and want to know more about R then do check out the R programming tutorial.

Browse Categories

...