I used following code to get the quantiles (25 %, 50 %,75 % and 99 %) of x and replicate 100 times.
x<-c(1,2,3,5,4,5,6,7,8,5,4,3,2)
sample.boot=numeric()
for (i in 1:100){
sample.boot[i]<-quantile(sample(x,replace = T),c(0.25,0.50,0.75,0.99))
}
sample.boot
This is not giving desired output. I want all four quantiles replicated 100 times and stored as data frame or in a matrix as below.
4 5 5 7
2 4 6 7
.......
.......
3 5 5 6
t(replicate(100, quantile(sample(x,replace = T),c(0.25,0.50,0.75,0.99))))
# 25% 50% 75% 99%
# [1,] 3 4 5 7.64
# [2,] 5 5 5 7.00
# [3,] 2 5 5 7.88
# [4,] 2 4 5 7.88
# [5,] 4 5 7 7.88
# [6,] 4 4 7 7.88
# [7,] 3 4 5 7.00
# [8,] 2 4 5 7.00
# [9,] 4 5 5 7.76
# [10,] 3 4 5 7.64
Internally replicate
uses sapply
and does so in a way that makes this type of repeated action efficient. The first argument accepts an integer value representing the number of times that the second argument should be repeated.
The entire function is wrapped with t(..)
. This arranges the output with quantiles as columns instead of rows.