- sample function
- sample takes a sample of the specified size from the elements of x using either with or without replacement.
> sample(1:6, 4, replace = TRUE)
[1] 5 3 3 2 #
duplicated number could be generated with replace option
sample without duplicated number
> sample(1:20, 10)
[1] 16 15 2 20 12 1 13 5 18 11
predefined English alphabet variable in R - LETTERS
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
sample 10 characters with "a" and "b" with probability of 50% respectively
> sample(c("a","b"), 10, replace = TRUE, prob = c(0.5, 0.5))
[1] "b" "a" "a" "b" "a" "a" "a" "a" "a" "a"
if we would like to generate 10 numbers with binominal data (0 or 1), we could use rbinom()
> rbinom(n = 10, size = 1, prob = 0.7)
[1] 1 0 1 1 0 0 0 1 1 1
replicate() creates matrix, (replicate the function for n times)
> replicate(10,rbinom(n = 10, size = 1, prob = 0.7))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 1 1 0 0 1 1 1 1
[2,] 1 1 1 1 1 1 1 1 1 1
[3,] 0 1 1 1 1 0 1 1 1 1
[4,] 1 0 1 1 0 1 1 1 0 1
[5,] 0 0 1 1 1 1 0 1 0 0
[6,] 1 0 1 0 1 1 0 1 1 0
[7,] 1 0 0 1 1 0 1 1 1 0
[8,] 0 1 1 0 1 1 1 1 1 0
[9,] 0 0 1 1 1 1 1 1 1 1
[10,] 1 0 1 1 1 1 0 0 0 1
No comments:
Post a Comment