create a data frame from named vector in R

Viewed 22

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.

1 Answers

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
Related