[R] Can I specify POSIX[cl]t column classes inside read.csv?

David Winsemius dwinsemius at comcast.net
Mon Apr 23 19:23:12 CEST 2012


On Apr 23, 2012, at 11:48 AM, Thomas Levine wrote:

> I'm loading a nicely formatted csv file.
>
>     #!/usr/bin/env Rscript
>     kpi <- read.csv(
>       # This is a dump of the username, date_joined and last_login  
> columns
>       # from the auth_user Django table.
>       'data/2012-04-23.csv',
>       colClasses = c('character')
>     )
>     print(kpi[sample(nrow(kpi), 3),2:3])
>
> Here's what the three rows I printed look like.
>
>              last_login         date_joined
>     2012-02-22 02:44:11 2011-09-19 03:07:35
>     2011-09-16 01:34:41 2011-09-16 01:34:41
>     2011-07-02 20:29:17 2011-07-02 20:29:17
>
> Once I load them, I'm converting the datetimes to datetimes.
>
>     kpi$last_login <- as.POSIXlt(kpi$last_login)
>     kpi$date_joined <- as.POSIXlt(kpi$date_joined)
>
> Can I do this inside of read.csv by specifying colClasses?


Possibly. If there is an "as" function for a particular class, it can  
be used in the colClasses vector of read.* functions. It appears that  
your input file might have the right combination of formats and  
separators for this to succeed.



> It's
> obviously not a problem if I can't; it just seems like I should be
> able to.
>
> Note that the following doesn't work because it doesn't save the  
> times.
>
>     colClasses = c('character', 'Date', 'Date')


Try instead:

colClasses = c('character', 'POSIXlt', 'POSIXlt')


-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list