Monday, February 2, 2015

R6. The function sample and quantile in R

The function sample selects values randomly from a vector, with or without replacement.


With replacement, we can get a vector of length greater than the input vector. We use the unique function to see how many values are unique.


The quantile function orders our vector and gives the number corresponding to a percentage.

# ex6.R
x <- sample(0:99,size = 300, replace = TRUE)
y <- length(x)-length(unique(x))
print(sprintf('The number of repeats are %d', y))
print(sprintf('The mean is %.2f and standard deviation is %.2f',
              mean(x), sd(x)))
print('The quantiles are:')
v <- seq(.25,.75,.1)
print(quantile(x, probs = v))
## [1] "The number of repeats are 205"
## [1] "The mean is 48.77 and standard deviation is 28.79"
## [1] "The quantiles are:"
## 25%   35%   45%   55%   65%   75% 
## 24.00 34.00 42.00 54.45 65.00 73.00 

No comments:

Post a Comment