calculate median from data.table columns in R -


i trying calculate median value across number of columns, data bit funky. looks following example.

library(data.table)  dt <- data.table("id" = c(1,2,3,4),"none" = c(0,5,5,3),                   "ten" = c(3,2,5,4),"twenty" = c(0,2,3,1))      id none ten twenty 1:  1    0   3      0 2:  2    5   2      2 3:  3    5   5      3 4:  4    3   4      1 

in table column represents number of occurrences of value. wanting calculate median occurrence.

for example id = 1

median(c(10, 10, 10)) 

is calculation wanting create.

for id = 2

median(c(0, 0, 0, 0, 0, 10, 10, 20, 20)) 

i have tried using rep() , lapply() limited success , after clear guidance on how might achieved. understand likes of rep() having hard code value repeated (e.g. rep(0,2) or rep(10,2)) , expect. struggling create list or vector repetitions each column.

here's data.table way (assuming unique id):

dt[, median(rep(c(0, 10, 20), c(none, ten, twenty))), by=id] #    id v1 # 1:  1 10 # 2:  2  0 # 3:  3 10 # 4:  4 10 

this attempt @eddi's answer without reshaping (which tend use last resort).


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo