Search code examples
rdplyrformatquantilecdf

How to create a datatable based on multiple variables in R?


I have 6 variables that I made based on the quantile function (I created different quantiles on top of the default amount of '0.25, .5, and .75'. I would like to create a summary table similar to the output below but with my variables. Any help would be much appreciated.

I used cbind to merge all the variables together in a table but the format is off.


ten_quantile = quantile(tt$HPTACC, probs = .1)
twenty_quantile = quantile(tt$HPTACC, probs = .2)
thirty_quantile = quantile(tt$HPTACC, probs = .3)
med = quantile(tt$HPTACC, probs = .5)
sixty_quantile = quantile(tt$HPTACC, probs = .6)
upper = quantile(tt$HPTACC, probs = .75)


rr <- cbind(ten_quantile, twenty_quantile, thirty_quantile, med, sixty_quantile, upper)
rr

ten_quantile twenty_quantile thirty_quantile   med sixty_quantile   upper
10%       7172.1        17137.72         22099.3 29542          32302 35864.5

However I would like to have something like this but with the additional quantiles:


Sfttime <- quantile(tt$HPTACC)


## Create quantile soft time into df ##
SummaryTable <- data.frame(
  Sfttime)

      Sfttime
0%        0.00
10%   #
20%   #
25%   20072.25
35%   #
50%   29542.00
60%   #
75%   35864.50
100% 889368.00

Solution

  • I think you can use the quantile() function. You can configure the values in the probs argument. I will use mtcars as an example, but it should work with your data

    QuantData <- quantile(mtcars$drat)
    QuantData <- as.data.frame(QuantData)
    
    Result <- data.frame(Quantile = row.names(QuantData), 
                         Value = QuantData[,1])
    
    QuantData <- quantile(mtcars$drat, 
                          probs = c(0,0.1,0.20,0.25,0.35,0.5,0.6,0.75,1))
    QuantData <- as.data.frame(QuantData)
    
    Result <- data.frame(Quantile = row.names(QuantData), 
                         Value = QuantData[,1])