[datatable-help] column values as index

Matthew Finkbeiner matthew.finkbeiner at gmail.com
Wed May 30 12:17:50 CEST 2012


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


More information about the datatable-help mailing list