有没有办法在read.table/read.csv中使用colClasses参数时指定Date格式?
(我意识到我可以在导入后进行转换,但是有很多像这样的日期列,在导入步骤中更容易实现)
我有一个.csv格式的日期列%d/%m/%Y.
dataImport <- read.csv("data.csv", colClasses = c("factor","factor","Date"))
Run Code Online (Sandbox Code Playgroud)
这会导致转换错误.例如,15/07/2008成为0015-07-20.
data <-
structure(list(func_loc = structure(c(1L, 2L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 5L), .Label = c("3076WAG0003", "3076WAG0004", "3076WAG0007",
"3076WAG0009", "3076WAG0010"), class = "factor"), order_type = structure(c(3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 1L), .Label = c("PM01", "PM02",
"PM03"), class = "factor"), actual_finish = structure(c(4L, 6L,
1L, 2L, 3L, 7L, 1L, 8L, 1L, 5L), .Label = c("", "11/03/2008",
"14/08/2008", …Run Code Online (Sandbox Code Playgroud) 正如标题所说.为什么润滑剂的功能要慢得多?
library(lubridate)
library(microbenchmark)
Dates <- sample(c(dates = format(seq(ISOdate(2010,1,1), by='day', length=365), format='%d-%m-%Y')), 50000, replace = TRUE)
microbenchmark(as.POSIXct(Dates, format = "%d-%b-%Y %H:%M:%S", tz = "GMT"), times = 100)
microbenchmark(dmy(Dates, tz ="GMT"), times = 100)
Unit: milliseconds
expr min lq median uq max
1 as.POSIXct(Dates, format = "%d-%b-%Y %H:%M:%S", tz = "GMT") 103.1902 104.3247 108.675 109.2632 149.871
2 dmy(Dates, tz = "GMT") 184.4871 194.1504 197.8422 214.3771 268.4911
Run Code Online (Sandbox Code Playgroud)