[datatable-help] Join and replacement

DUPREZ Cédric Cedric.DUPREZ at ign.fr
Mon Jun 18 12:56:36 CEST 2012


Dear all,

I'm trying to replace values within a data.table object with values comming from another data.table object, joining them together.

Here is an example:
DT <- data.table("id"=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
        , val=c(0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 2, 0, 1, 0)
        , key = "id")
DT2 <- 	data.table("id"=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
        , val=c(3, 3, 3, 5, 5, 5, 5, 3, 6, 4, 3, 2, 3, 5, 4, 4)
        , key = "id")
I would like to replace all val = 0 in DT with the corresponding DT2 values, joining DT and DT2 on "id".
That is, get the following DT :
      id val
 [1,]  1   3
 [2,]  2   3
 [3,]  3   1
 [4,]  4   1
 [5,]  5   2
 [6,]  6   2
 [7,]  7   5
 [8,]  8   3
 [9,]  9   1
[10,] 10   1
[11,] 11   2
[12,] 12   2
[13,] 13   2
[14,] 14   5
[15,] 15   1
[16,] 16   4

I tried 
X <- DT[DT2][val==0,]
DT[X, val:=val.1]
Or DT[X, val:=val.1, roll = T]

But I always get the following error message: "combining bywithoutby with := in j is not yet implemented."

The following code seems to work:
DT[X]$val <- DT[X]$val.1
But I get a warning message: "In `[<-.data.table`(`*tmp*`, X, value = list(id = c(1L, 2L, 7L,  :
  Supplied 2 columns to be assigned a list (length 4) of values (2 unused)"

Any suggestion to help me?

Thanks a lot for your help,

Cedric


More information about the datatable-help mailing list