[datatable-help] Join and replacement

Matthew Dowle mdowle at mdowle.plus.com
Mon Jun 18 13:11:32 CEST 2012


Hi,

You'll need 1.8.1 for that :
install.packages("data.table", repos="http://R-Forge.R-project.org")

Also check live NEWS link at the top of ?data.table, as there's quite a
lot in this version.

Also this recent question is related :
http://stackoverflow.com/questions/11030614/how-to-best-join-one-column-of-a-data-table-with-another-column-of-the-same-data

Matthew


> 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
> _______________________________________________
> 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