Aggregate data.table with weights in R -


i new using data.table, , confusing. have dataset looks (data.table)

geo    anc   wg  v1  .... p1      a1  w1  v11 .... p1      a2  w2  v12 .... p2      a1  w3  v13 .... p3      a1  w4  v14 .... 

here's reproducible example:

set.seed(123) dt <- data.table(geo = c("b","b","b","b","b","a","a","c"),                  anc = c("x","y","z","x","x",na, "x","w"),                   wg=sample(100,8),                   c=sample(5,8, replace=t)) 

the variable wg gives weight of each observation.

i want compute weighted frequency of an each location geo, in fastest possible way , gives output can easily merge data (hence not using tapply). have done far is:

dt[,lapply(.sd, function(n){wpct(n,dt[["wg"]],na.rm=t)}),                              by=c("geo"),.sdcols=c("an")] 

it seems work fine, output gives 2 columns: geo , an, , frequency under variable an.

geo       p1      fre1   p1      fre2   p2      fre3 p3      fre4 

i prefer output 3 columns: geo, an (with real values a1, a2, , forth), , third column frequencies. i'll merge data original one.

any ideas on how this?


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -