Random Sampling in R

The sample() function draws randomly from a specified set of (scalar) objects allowing you to sample from arbitrary distributions of numbers.

Draw 10 samples from 1 to 100.

 

set.seed(1)
sample(1:100, 10)

Output:

[1] 27 37 57 89 20 86 97 62 58 6

Doesn’t required to be numbers always. Draw 10 letters from a to z.

 

sample(letters, 10)

Output:

[1] "f" "e" "q" "i" "x" "k" "o" "s" "g" "n"

You can do a random permutation using sample().

sample(1:5)
sample(1:10)

Output:

> sample(1:5)
[1] 5 1 2 4 3
> sample(1:10)
[1] 4 1 10 7 3 6 5 2 9 8

Sample with replacement:

 

sample(1:10, replace = TRUE)

Output:

[1] 7 8 2 8 5 9 7 8 6 6

Sample Rows from a Data Frame:

For this purpose we will use PlantGrowth dataset which is a built-in dataset in R.

 

library(datasets)
data(PlantGrowth)
head(PlantGrowth)

Output:

  weight group
1 4.17 ctrl
2 5.58 ctrl
3 5.18 ctrl
4 6.11 ctrl
5 4.50 ctrl
6 4.61 ctrl

Create index vector:

 

set.seed(20)
id <- seq_len(nrow(PlantGrowth))
id

Now you can take sample from the index vector.

s <- sample(id, 10)

PlantGrowth[s,]

Output:

  weight group
27 4.92 trt2
23 5.54 trt2
8 4.53 ctrl
15 5.87 trt1
26 5.29 trt2
25 5.37 trt2
3 5.18 ctrl
2 5.58 ctrl
28 6.15 trt2
22 5.12 trt2

Random Number Seed in R

Data Visualization Using R