[datatable-help] problems with adding a DateTime column

Alexander Peterhansl APeterhansl at GAINCapital.com
Thu Mar 8 18:31:04 CET 2012


Case in point (and same for data.frame):

data.table(A=c(1,2,3),C= strptime(c("2011-01-01","2011-01-02","2011-01-03"),format="%Y-%m-%d"))

produces
        A          C
[1,] 1 2011-01-01
[2,] 2 2011-01-02
[3,] 3 2011-01-03



-----Original Message-----
From: datatable-help-bounces at r-forge.wu-wien.ac.at [mailto:datatable-help-bounces at r-forge.wu-wien.ac.at] On Behalf Of Alexander Peterhansl
Sent: Thursday, March 08, 2012 12:19 PM
To: Matthew Dowle; Chris Neff
Cc: datatable-help at lists.r-forge.r-project.org
Subject: Re: [datatable-help] problems with adding a DateTime column

Thanks very much.  I never realized that strptime() was returning POSIXlt type -- definitely something to avoid.  Using as.POSIXct() instead fixes the issue, though probably not the best way to go about it.

Alex 

 

-----Original Message-----
From: Matthew Dowle [mailto:mdowle at mdowle.plus.com]
Sent: Thursday, March 08, 2012 11:44 AM
To: Chris Neff
Cc: Alexander Peterhansl; datatable-help at lists.r-forge.r-project.org
Subject: Re: [datatable-help] problems with adding a DateTime column

Ah, we crossed emails. It's been reported (to me offlist) that POSIXct may be working in keys, in integer form without any fractional seconds I guess. Worth a try.

> Try as.IDate around your strptime.  strptime returns POSIXlt which 
> isn't really amenable to data tables (or frames for that matter).  In 
> base R you would usually convert to POSIXct, but IDate is integer 
> based and made to be able to key on dates in data.tables.
>
> On Thu, Mar 8, 2012 at 11:11 AM, Alexander Peterhansl 
> <APeterhansl at gaincapital.com> wrote:
>> Dear Datatable-Help:
>>
>> I'm running R version 2.14.1 and data.table 1.7.10 (on Windows 7 and 
>> Linux).  On both systems I get a strange result in the following toy
>> example:
>>
>>
>>
>> test <- data.table(A=c(1,2,3))
>>
>> test[,B:= c("2011-01-01","2011-01-02","2011-01-03")]
>>
>> test[,C:=
>> strptime(c("2011-01-01","2011-01-02","2011-01-03"),format="%Y-%m-%d")
>> ]
>>
>> Output:
>>
>>       A          B       C
>>
>> [1,]   1 2011-01-01 0, 0, 0
>>
>> [2,]   2 2011-01-02 0, 0, 0
>>
>> [3,]   3 2011-01-03 0, 0, 0
>>
>>
>>
>> Columns A and B show the expected behavior.  Column C was supposed to 
>> simply convert the date strings into DateTime types.  Clearly this is 
>> not happening.
>>
>>
>>
>> Please help.
>>
>> Regards,
>>
>> Alex
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> datatable-help mailing list
>> datatable-help at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatabl
>> e-help
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable
> -help
>


_______________________________________________
datatable-help mailing list
datatable-help at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help


More information about the datatable-help mailing list