Let's say there is the vector of date/times in R:
l<-c("2011-01-01 00:00:00", "2011-01-01 01:00:00", "2011-01-01 02:00:00")
I would like to add the certain amount of time(f.e. 1 hour) to each element of this vector. First of all, I converted the elements to POSIXlt:
l1<-as.POSIXlt(l)
then I tried to use sapply and add hours as described in How to add/subtract time from a POSIXlt time while keeping its class in R?:
f<-function(dt, hour){
dt$hour<-dt$hour + hour
return(dt)
}
sapply(l1, function(x) f(x,1))
However, executing this code gives the error: Error in dt$hour : $ operator is invalid for atomic vectors
Debugging tells that class(dt) is numeric and not POSIXt
How can I solve this problem?