我正在尝试将格式为字符的日期字段导入到POSIXlt
类中,同时导入数据本身。我不想将日期作为字符导入并转换为POSIXlt
以后,而是想一步完成,因为我的数据很大(4+ GB)。下面是相同的代码。
在这段代码中,我定义了一个新的“myDate”类并使用它来传递colClasses
参数。此方法适用于pkg read.delim
,但不适用于pkg。fread
data.table
如果可以使用它会很棒,fread
因为如果可行,我将使用该方法节省至少 20 分钟的时间fread
。您可以使用它fread
或提出更好的替代方案吗?
library (data.table)
library (lubridate)
setClass ('myDate')
# create custom 'myDate' class. Having "Date" or "POSIXlt" in colClasses argument of read.table does not work
setAs ("character","myDate", function(from) as.POSIXlt(fast_strptime(from, "%Y-%m-%d")) )
ccs <- c ("logical", "myDate", "myDate", "character")
# fread() Does NOT Work
rawdata <- fread ("filepath/filename.txt", colClasses=css, sep='|', header=FALSE,))
# WORKS!
rawdata <- read.delim ("filepath/filename.txt", colClasses=ccs, sep='|', header=FALSE)