Quantcast

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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

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

Thomas Levine-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

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

David Winsemius

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

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

Jeff Newmiller
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:<[hidden email]>        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 <[hidden email]> 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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...