Search code examples
rquantile

Transform quantiles output into a table


I have this code

tapply(index_articles_migpunts$index_articles_ponderats2_migpunts, index_articles_migpunts$Tramspoblacio, quantile, probs=seq(.1,.9, by =.1)) 

which gives this output:

$`1 - 999`
       10%        20%        30%        40%        50%        60%        70%        80% 
 0.0000000  0.8333333  5.0000000  5.0000000  5.8333333  7.8666667 10.0000000 13.3500000 
       90% 
17.1166667 

$`1000 - 4999`
      10%       20%       30%       40%       50%       60%       70%       80%       90% 
 5.833333 10.866667 13.300000 16.333333 17.479167 19.200000 20.687500 23.091667 28.166667 

$`5000 - 9999`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
11.66667 15.08333 19.16667 21.00000 24.00000 27.33333 30.75000 36.29167 56.75000 

$`10000 - 19999`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
14.85000 18.36667 21.25000 24.13333 26.91667 30.83333 34.84167 39.95000 60.90000 

$`20000 - 39999`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
23.22500 27.66667 31.24167 34.43333 37.75000 41.90000 45.46667 50.41667 66.04167 

$`40000 - 79999`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
34.85417 38.54167 41.14583 44.16667 46.58333 51.00000 54.72917 57.95833 75.60417 

$`80000 - 200000`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
39.11667 41.72500 44.36667 47.22500 49.87500 54.76667 56.66667 58.27500 88.57500 

$`Mayor de 200000`
     10%      20%      30%      40%      50%      60%      70%      80%      90% 
45.92917 50.25000 52.75000 53.02500 55.64583 56.11667 63.25833 66.32500 95.31667 

How can I transform this console output into a data.frame or a table retaining the distinct categories names?

Thank you very much!


Solution

  • something along the lines of the below should do the job. Your result is a named list, so bind the entries into rows in a data.frame using rbind and do.call (or use bind_rows(res) from the dplyr package). Afterwards add the intervals using the names in the result list.

    res <- tapply(index_articles_migpunts$index_articles_ponderats2_migpunts, index_articles_migpunts$Tramspoblacio, quantile, probs=seq(.1,.9, by =.1))
    df <- do.call(rbind, res)
    df$intervals <- names(res)