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

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Apr 23 23:52:34 CEST 2012


I recommend not putting POSIXlt vectors in data frames because of memory use and added complexity of the resulting data frame.  That is, use

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

instead. The POSIXlt values will still be created as temporary variables for reading in, but the data frame will store only the simpler and more compact type for later use.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

David Winsemius <dwinsemius at comcast.net> wrote:

>
>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
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list