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

Thomas Levine perluette at thomaslevine.com
Mon Apr 23 17:48:28 CEST 2012


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? 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')

Thanks

Tom



More information about the R-help mailing list