r - How to create function for data.table -


i want create function used data.table. supposed have

library(data.table)     dt = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)      foo <- function(data, field, grp){       data[, field, by=grp]     } 

i have tried

   foo(dt, .n, grp = y) 

or

   foo(dt, y) 

they return errors. how pass input arguments in data.table?

you in essence asking reinvent function [.data.table. [ function perform implicit evaluation of second argument, j, in context of datatable. in case of getting counts groups it's just:

dt[ ,.n, by=y]    y n 1: 1 3 2: 3 3 3: 6 3 

had wanted sequences groups have been:

> dt[ ,1:.n, by=y]    y v1 1: 1  1 2: 1  2 3: 1  3 4: 3  1 5: 3  2 6: 3  3 7: 6  1 8: 6  2 9: 6  3 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -