Percentile for Each Observation w/r/t Grouping Variable

后端 未结 7 1361
广开言路
广开言路 2021-02-06 10:39

I have some data that looks like the following. It is grouped by variable \"Year\" and I want to extract the percentiles of each observation of Score, with respect to t

7条回答
  •  攒了一身酷
    2021-02-06 11:07

    Using data.table is pretty straight-forward as well. Just for completeness and also as an easy way to find the data.table solution.

    library(data.table)
    year <- rep(2001:2005, 2)
    score <- round(rnorm(10, 35, 3))
    
    dt <- data.table(score)
    
    
    dt[, .(Percentile = ecdf(score)(score)), by = list(year)]
    

提交回复
热议问题