[datatable-help] J() casts to int?

Johann Hibschman jhibschman+r at gmail.com
Tue Oct 4 17:03:15 CEST 2011


I just noticed that J casts all its arguments to int.  Has this always
been the case?  I can't find it documented anywhere.

I came across this while trying to do a self join, like this:

  > tmp <- data.table(date=1:5, value=10*rnorm(5), key="date")
  > tmp
       date     value
  [1,]    1  3.710278
  [2,]    2  4.571288
  [3,]    3  2.009627
  [4,]    4  8.237882
  [5,]    5 -9.004814
  > with(tmp, J(date, value))
       date value
  [1,]    1     3
  [2,]    2     4
  [3,]    3     2
  [4,]    4     8
  [5,]    5    -9
  > tmp[J(date + 2, prev.date=date, prev.value=value),
        list(prev.date, value, prev.value, delta=value-prev.value)]
       date prev.date     value prev.value       delta
  [1,]    3         1  2.009627          3  -0.9903734
  [2,]    4         2  8.237882          4   4.2378817
  [3,]    5         3 -9.004814          2 -11.0048141
  [4,]    6         4        NA          8          NA
  [5,]    7         5        NA         -9          NA
  > tmp[data.table(date + 2L, prev.date=date, prev.value=value),
        list(prev.date, value, prev.value, delta=value-prev.value)]
       date prev.date     value prev.value      delta
  [1,]    3         1  2.009627   3.710278  -1.700652
  [2,]    4         2  8.237882   4.571288   3.666594
  [3,]    5         3 -9.004814   2.009627 -11.014441
  [4,]    6         4        NA   8.237882         NA
  [5,]    7         5        NA  -9.004814         NA

Is this intended?  Using J let me be sloppy and do "+2" while data.table
made me use "+2L", but then it clobbered the non-int values.

Is there a better way?

Thanks,
Johann



More information about the datatable-help mailing list