Using quantiles in R

Is using QUANTILE function in R an exact way to get 80th and 90th percentile?

set.seed(42)
vars <- sample(1:1000,100)
sort(vars[which(vars >=  quantile(vars,.9))])
#>  [1] 965 967 973 974 984 986 989 994 995 997

Same idea

set.seed(42)
vars <- runif(100,min = 1, max = 100)
sort(vars[which(vars >=  quantile(vars,.9))])
#>  [1] 93.53255 93.77047 94.06144 94.72016 95.80008 97.12569 97.38045 97.84442
#>  [9] 98.29890 98.90028

This looks like a homework problem. Please pay attention to the homework policy. I'm also a bit confused by the post title that mentions "loop" but your post mentions making a "function". Which is it? What have you tried?

1 Like