It seems I have never been able to use lapply
properly and I've decided to put an end to that today. I'm sure my question is basic, but I've searched SO quite thorouly, as well as reference documents, and can't find an answer.
I have 4 dataframes with multiple columns, one of which contains date and time information (Datetime) currently stored as factors (thousands of levels of type "2016-01-01 00:00:00")
I can easily change the format on a single dataframe, using:
df$Datetime<-as.POSIXct(strptime(as.character(df$Datetime), "%Y-%m-%d %H:%M:%S", tz="America/Edmonton"))
Here is my attempt at using lapply
to do the same thing on all the dataframes simultaneously.
Datasets<-list(Data2017_1h, Data2016_1h, Data2017_15min, Data2016_15min)
Datasets<-lapply(Datasets, function(x){
as.POSIXct(strptime(as.character(x$Datetime), "%Y-%m-%d %H:%M:%S", tz="America/Edmonton"))
})
The output of that is a list of length 4 with each element of type "Double (S3:POSIXct, POSIXt)". I loose all the other variables (there are originally 16 columns) and if I try to view one element of one list, it comes out as Double, not POSIXct.
I'm quite confused, so I'd be grateful for any insight. I'm sorry I don't have a reproducible example, I'm not quite there in my learning curve.
Not sure how to fully answer your question since there is no data to replicate. But you could just try:
lapply(Datasets, function(x) as.POSIXct(x$Datetime, format = "%Y-%m-%d %H:%M:%S", tz="America/Edmonton")
This has worked without specifying class type.
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.