sample(x = 1:10, size = 4, replace = F)#> [1] 4 8 7 9
The function sample() is used to generate random values from a vector, and it has the following arguments:
x size replace TRUE or FALSE
prob x
sample(x = 1:10, size = 4, replace = F)#> [1] 4 8 7 9
Select 10 numbers from 0 to 100
sample(x = 0:100, size = 10, replace = F) # replace=FALSE#> [1] 73 65 57 85 51 2 72 55 80 86
sample(x = 0:100, size = 10, replace = T) # replace=TRUE#> [1] 75 29 76 85 42 13 32 6 31 43
sample(replace = TRUE, x = LETTERS[1:4], size = 10)#> [1] "C" "B" "B" "C" "D" "C" "D" "D" "B" "A"
rbinom() and rnorm
rbinom() is used to draw a sample from a binomial distribution
size
prob
n
Draw a sample of size 8 from
rbinom(size = 10, prob = .75, n = 8)#> [1] 9 8 8 6 8 7 9 7
rnorm() is used to draw a sample from a normal distribution
mean
sd
n
Draw a sample of size 5 from
rnorm(mean = 10, sd = 4, n = 5)#> [1] 14.743527 8.970948 11.748854 8.539669 11.986696
pnorm()For
dnorm()dbinom(x = 5, size = 10, prob = 0.5)#> [1] 0.2460938
qnorm()qnorm(p = 0.975, mean = 0, sd = 1)#> [1] 1.959964