I am trying to import date fields which are formatted as a character, into a POSIXlt
class while importing the data itself. Instead of importing the date as character and converting to POSIXlt
later, I want to do this in one step, since my data is quite big (4+ GB). Below is the code for the same.
In this code, I define a new 'myDate' class and using it to pass into the colClasses
argument. This method works with read.delim
, but not on fread
from data.table
pkg.
It would be great if this is made to work with fread
because, I will save at least 20 Min of time with fread
method, if this works. Can you make it to work with fread
or suggest a better alternative ?
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)