2

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.

4

1 回答 1

2

您可以col_types在读取文件时尝试使用该参数,也可以在之后进行转换:

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

或者,如果它是,如你所说,一个字符串......

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

更好的是,为了保持时间,尝试:

library(lubridate)
data$datecol <- as_datetime(as.numeric(data$datecol)*3600*24, origin='1900-01-01')
于 2018-05-17T03:15:39.977 回答