How to get samples from a data set in R


  R Interview Questions

SAMPLE () function

# Generates 10 random numbers between 1 and 100
> sample(1:100,10)
 [1] 31 26 55  6 45 46 77 35 51 16

You can do samples with replacement as well.

# Generate 10 random numbers between 1 and 100 with replacement
> sample(1:100,10, replace = TRUE)
 [1] 54 72 54 75 43 18 78 89 55 28

Sampling is also used to generate training and test data from a dataset.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.