1

I am trying to implement the data.table method described here: Calculating grouped variance from a frequency table in R.

I can successfully replicate their example. But when I apply it to my own data, nothing seems to happen. In particular, output is this:

table <- data.frame(districts,proportions,populations)
table<-setDT(table)
      districts proportions populations
   1:        24   0.8270270        1269
   2:        26   0.8867925        1679
   3:        12   0.9136691         510
   4:        27   0.4220532        3274
   5:        20   0.5457650        3644
 ---                                  
8937:         1   0.7798072        3444
8938:         1   0.6080247        6128
8939:         1   0.4655172        4335
8940:         1   0.4813200        4297
8941:         1   0.7690167        3906     


setDT(table)[, list(GroupMedian=as.double(median(rep(proportions, populations))),
                    TotalCount=sum(populations)) , by = districts]
print(table)

##Same output as above###

I have no idea whats going on, after much time.

user434180
  • 735
  • 1
  • 5
  • 9
  • 1
    The command you show doesn't modify `table`, try storing the result in a new variable: `result <- table[, list(GroupMedian = ...)]`, then looking at `result`. – Gregor Thomas Dec 13 '18 at 19:34
  • You havent changed anything in your original "table". So when you print "table" you simply get the original table. You computed the median by districts on "table" but never assigned the results to a new variable. Also `setDT()` converts your data.frame to a data.table object, so no need for `table <- setDT(table)`. Nonetheless `setDT(table)[, list(GroupMedian=as.double(median(rep(proportions, populations))), TotalCount=sum(populations)) , by = districts]`should print the desired result, doesnt it? – mmn Dec 13 '18 at 19:53
  • Ahh yes, very silly of me indeed. Thanks very much! – user434180 Dec 17 '18 at 23:23

0 Answers0