[datatable-help] IDateTime and plotting thereof

Matthew Dowle mdowle at mdowle.plus.com
Fri Dec 16 09:50:36 CET 2011


Hi.

You should be able to add more than one column in one step using := by
setting with=FALSE like this : 

> DT = data.table(a=1:3,b=4:6)
> DT
     a b
[1,] 1 4
[2,] 2 5
[3,] 3 6
> DT[,c("newcol1","newcol2"):=list(7:9,3.14)]
Error in `[.data.table`(DT, , `:=`(c("newcol1", "newcol2"),
list(7:9,  : 
  LHS of := must be a single column name, when with=TRUE. When
with=FALSE the LHS may be a vector of column names or positions.

so set with=FALSE :

> DT[,c("newcol1","newcol2"):=list(7:9,3.14),with=FALSE]
Error in `[.data.table`(DT, , `:=`(c("newcol1", "newcol2"),
list(7:9,  : 
  Logical error. Can only add new columns one at a time currently
> 

I intended that to work so I'll take a look. But, the intention is at
some point to enable this :

   DT[,{ newcol1 := ....
         newcol2 := ....
         newcol3 := newcol1 + newcol2
       }, by=... ]

where not only can you add multiple columns by reference and by group,
but they can 'see' previous columns just added. That's going to be quite
tricky to implement so don't hold your breath!

In the meantime it's either one at a time with :=, or use (slower) [<-
method to add multiple columns (via at least one full table copy).

Re plotting, I'm hoping to learn ggplot2 myself soon, so bear that in
mind, but this is the way I thought should work :

 ape.ph[,qplot(date, timei)]

If that gives the same error I'm not sure why it would try to coerce
ITime into a data.frame. Does anyone else know?  If you can provide some
example data it might help save us time to reproduce it.

Matthew



On Thu, 2011-12-15 at 19:25 -0500, Farrel Buchinsky wrote:
> I was able to take a column of dates and times that looked like this 
> 
> 2011 NOV18 09:29:16
> 
> 
> I then tried this
> 
> ape.ph[,date:=IDateTime(strptime(ape.ph$time,"%Y %b%d %H:%M:%S"))[[1]]]
> ape.ph[,timei:=as.ITime(strptime(ape.ph$time,"%Y %b%d %H:%M:%S"))]
> 
> 
> I had to create one column called date to accept the first column of
> the IDateTime output. There does not appear to be a way to use the
> IDateTime function and take both of its columns and stick it into the
> data.table since one can only add one new column at a time as far as I
> can tell. Anyway, I then created a variable timei to accept the time.
> 
> 
> I wanted to plot the times that something happened on the y axis and
> then on the x axis I wanted to have the various dates
> 
> 
> library(ggplot2)
> qplot(ape.ph[,date], ape.ph[,timei])
> 
> 
> but alas I got this
> 
> 
> Error in as.data.frame.default(x[[i]], optional = TRUE) : 
> cannot coerce class '"ITime"' into a data.frame
> 
> 
> I am eventually hoping for this
> qplot(ape.ph[,date], ape.ph[,timei]) + 
>    scale_y_datetime(format="%H:%M:%S") +
>            xlab("Day") + ylab("Hour")
> 
> 
> Do you have any examples of dates and times used in a data.table and
> then being used to plot graphs? I would love to see some examples. I
> could not find any
> 
> 
> 
> 
> Farrel Buchinsky
> Google Voice Tel: (412) 567-7870
> 
> 
> _______________________________________________
> 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