Gaurav Bansal
Gaurav Bansal

Reputation: 5660

Convert columns to posixct using sapply and keeping datetime format in R

I want to use sapply (or something similar) to convert certain columns to POSIXct in an R data.frame but maintain the datetime format of the columns. When I do it currently, it converts the format to numeric. How can I do this? An example is below.

#sample dataframe
df <- data.frame(
  var1=c(5, 2),
  char1=c('he', 'she'),
  timestamp1=c('2019-01-01 20:30:08', '2019-01-02 08:27:34'),
  timestamp2=c('2019-01-01 12:24:54', '2019-01-02 10:57:47'),
  stringsAsFactors = F
)

#Convert only columns with 'timestamp' in name to POSIXct
df[grep('timestamp', names(df))] <- sapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))
df
  var1 char1 timestamp1 timestamp2
1    5    he 1546392608 1546363494
2    2   she 1546435654 1546444667

Note: I can use as.posixlt instead of as.posixctand it works, but I want the data in POSIXct format. I also tried converting to POSIXlt first and then to POSIXct, but that also ended up converting the columns to numeric.

Upvotes: 3

Views: 977

Answers (1)

MrFlick
MrFlick

Reputation: 206177

Use lapply rather than sapply. The "s" in sapply is for simplify and it turns the result into a matrix but sapply can't create a matrix of POSIXct values so it gets cast to a simple numeric matrix. But if you keep it a list, you don't lose the class.

df[grep('timestamp', names(df))] <- lapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))

You could also do this fairly easily with dplyr

library(dplyr)
df %>% mutate_at(vars(contains("timestamp")), as.POSIXct)

Upvotes: 3

Related Questions