skittlebug
skittlebug

Reputation: 29

R automatically converts dates to string when importing xlsx

I have an excel file with a column of date info. These are the dates that people took a survey, and are formatted like "1/20/2017 2:22:34 PM" as downloaded from Qualtrics.

But when I import this file to R using read_excel, each date automatically gets converted to a string like "43122.82".

Ultimately I want these dates to be Date types. I'd appreciate any help.

Upvotes: 2

Views: 705

Answers (1)

Dominic Comtois
Dominic Comtois

Reputation: 10401

You could either try using the col_types argument when reading the file, or do the conversion afterwards:

data$datecol <- as.Date(data$datecol, origin = "1900-01-01")

or if it's, as you say, a string...

data$datecol <- as.Date(as.numeric(data$datecol), origin = "1900-01-01")

Better even, to keep the time, try:

library(lubridate)
data$datecol <- as_datetime(as.numeric(data$datecol)*3600*24, origin='1900-01-01')

Upvotes: 2

Related Questions