Percentile for Each Observation w/r/t Grouping Variable

后端 未结 7 1218
广开言路
广开言路 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:26

    You can also do something like this:

    # first I'll create two dummy variables (Year, Score)
    year <- rep(2001:2005, 2)
    score <- round(rnorm(10, 35, 3))
    
    # then coerce variables to data frame
    d <- data.frame(year, score)
    
    # then you can use split() function to apply
    # function to each stratum of grouping variable
    sapply(split(score, year), function(x) quantile(x, probs=seq(.1, .9, .1)))
    

    Output will go something like this:

         2001 2002 2003 2004 2005
    10%  34.3 32.1 34.3 29.6 36.1
    20%  34.6 32.2 34.6 30.2 36.2
    30%  34.9 32.3 34.9 30.8 36.3
    40%  35.2 32.4 35.2 31.4 36.4
    50%  35.5 32.5 35.5 32.0 36.5
    60%  35.8 32.6 35.8 32.6 36.6
    70%  36.1 32.7 36.1 33.2 36.7
    80%  36.4 32.8 36.4 33.8 36.8
    90%  36.7 32.9 36.7 34.4 36.9
    

    You can utilize t() function to transpose rows and columns if you prefer. Writing a function will be a good way to tackle this kind of problems. I strongly recommend plyr package written by Hadley Wickam.

    Hope this helps! All the best!

提交回复
热议问题