R data.table column names not working within a function -


i trying use data.table within function, , trying understand why code failing. have data.table follows:

dt <- data.table(my_name=c("a","b","c","d","e","f"),my_id=c(2,2,3,3,4,4)) > dt    my_name my_id 1:           2 2:       b     2 3:       c     3 4:       d     3 5:       e     4 6:       f     4 

i trying create pairs of "my_name" different values of "my_id", dt be:

var1 var2        c    d    e    f b    c b    d b    e b    f c    e c    f d    e d    f 

i have function return pairs of "my_name" given pair of values of "my_id" works expected.

get_pairs <- function(id1,id2,tdt) {     return(expand.grid(tdt[my_id==id1,my_name],tdt[my_id==id2,my_name])) } > get_pairs(2,3,dt) var1 var2 1       c 2    b    c 3       d 4    b    d 

now, want execute function pairs of ids, try finding pairs of ids , using mapply get_pairs function.

> combn(unique(dt$my_id),2)      [,1] [,2] [,3] [1,]    2    2    3 [2,]    3    4    4 tid1 <- combn(unique(dt$my_id),2)[1,] tid2 <- combn(unique(dt$my_id),2)[2,] mapply(get_pairs, tid1, tid2, dt) error in expand.grid(tdt[my_id == id1, my_name], tdt[my_id == id2, my_name]) :    object 'my_id' not found 

again, if try same thing without mapply, works.

get_pairs3(tid1[1],tid2[1],dt) var1 var2 1       c 2    b    c 3       d 4    b    d 

why function fail when used within mapply? think has scope of data.table names, i'm not sure.

alternatively, there different/more efficient way accomplish task? have large data.table third id "sample" , need of these pairs each sample (e.g. operating on dt[sample=="sample_id",] ). new data.table package, , may not using in efficient way.

why function fail when used within mapply? think has scope of data.table names, i'm not sure.

the reason function failing has nothing scoping in case. mapply vectorizes function, takes each element of each parameter , passes function. so, in case, data.table elements columns, mapply passing column my_name instead of complete data.table.

if want pass complete data.table mapply, should use moreargs parameter. function work:

res <- mapply(get_pairs, tid1, tid2, moreargs = list(tdt=dt), simplify = false) do.call("rbind", res)   var1 var2 1        c 2     b    c 3        d 4     b    d 5        e 6     b    e 7        f 8     b    f 9     c    e 10    d    e 11    c    f 12    d    f 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -