[datatable-help] column values as index
Matthew Dowle
mdowle at mdowle.plus.com
Wed May 30 12:54:40 CEST 2012
Yes but in base not in data.table :
arr[as.matrix(dt)]
See FAQ 2.14 for how and why that inspired data.table.
Matthew
> Hi, I have an array like this:
> arr<- array(c(1,2,2,1),c(2,2))
>
> and a data.table like this:
> dt<- data.table(a = c(1,1,2,2), b = c(1,2,1,2))
>
> I'd like to add a column 'c' to dt that uses columns 'a' and 'b' as
> indices to arr, like this:
> dt$c<- apply(dt,1,function(x) arr[x['a'],x['b']])
>
> That works fine, of course, but it is pretty slow and I need to do
> this several times in each analysis I run. Is there a faster
> data.table way to do this?
>
> sorry if this is such a basic question!
>
> Matthew
> _______________________________________________
> 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