Since I have to run this on quite a large dataset, I ran some benchmarks to compare the different possibilities. Actually @Dan's solution is quite fast. However using attr(dttm,'tzone')<-'UTC'
is slightly faster.
myfun1<-function(){
myPSX[length(myPSX) + 1] <- PSXappend
}
myfun2<-function(){
dttm<-c(myPSX,PSXappend)
attr(dttm,'tzone')<-'UTC'
}
library(lubridate)
myfun3<-function(){
dttm<-c(myPSX,PSXappend)
with_tz(dttm, "UTC")
}
myfun4<-function(){
dttm<-as.POSIXct(c(my_chr,'2017-08-09 06:00'),format='%Y-%m-%d %H:%M',tz='UTC')
}
microbenchmark::microbenchmark(myfun1(),myfun2(),myfun3(),myfun4())
Unit: microseconds
expr min lq mean median uq max neval
myfun1() 12.642 15.210 17.92005 16.9875 17.7780 59.654 100
myfun2() 11.852 13.827 16.39909 14.4200 15.8025 43.062 100
myfun3() 26.864 29.432 121.86874 30.8150 33.1850 5852.844 100
myfun4() 31.605 34.766 61.66142 36.3460 40.2970 2182.323 100