1

I have to calculate a quartiles for given values and calculated the quartiles using quartile() function in R like below

vc <- c(1,2,5,6,7,9,12,15,18,19,27)
quartiles <- quantile(vc, probs = seq(0,1,0.01))

The output from above code is

0%     1.0
1%     1.1
2%     1.2
3%     1.3
4%     1.5
.... and so on upto 100%.  //this output is named vector

I want to convert this output named vector to data frame with columns 'quartile' and 'pvalues' like below

    quartile    pvalues
    0            1.0
    1            1.1
    2            1.2
    3            1.3
    4            1.4
    .... so on upto 100. 

I want to convert 0%, 1% ... values as a integer in quartile column and vectors values to pvalues column.

Hmm
  • 105
  • 10
  • One potential solution to get your specific output is `library(tidyverse); df <- data.frame("pvalues" = quartiles) %>% rownames_to_column("quartile") %>% mutate(quartile = parse_number(quartile)); df` – jared_mamrot Jul 13 '22 at 11:08

1 Answers1

0

We can use

vc <- c(1,2,5,6,7,9,12,15,18,19,27)
quartiles <- quantile(vc, probs = seq(0,1,0.01))
df <- data.frame(names(quartiles), quartiles, row.names = NULL)
colnames(df) <- c("quartile", "pvalue")
df$quartile <- as.numeric(gsub("%","", df$quartile))
> df
    quartile pvalue
1          0    1.0
2          1    1.1
3          2    1.2
4          3    1.3
5          4    1.4
6          5    1.5
gaut
  • 5,771
  • 1
  • 14
  • 45