Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(plyr)
- library(dplyr)
- library(tidyr)
- library(magrittr)
- # data generation
- set.seed(100)
- dt = data.table(t(replicate(100, sample(letters, 5)))) %>%
- setnames(paste0("id", 1:5))
- mapTable = data.table(value = letters, score = sample(1:100, 26))
- # solution
- dt %>% add_rownames %>% tbl_dt %>% gather(id, value, -rowname) %>%
- mutate(score = mapvalues(value, mapTable$value, mapTable$score)) %>%
- group_by(rowname) %>% summarise(med_score = median(score))
- # Source: local data table [100 x 2]
- #
- # rowname med_score
- # 1 1 73
- # 2 2 40
- # 3 3 54
- # 4 4 42
- # 5 5 43
- # 6 6 58
- # 7 7 56
- # 8 8 38
- # 9 9 58
- # 10 10 58
- # .. ... ...
Advertisement
Add Comment
Please, Sign In to add comment