R Language: Random Sampling

0

Random sampling is a statistical method in which each member of a population has an equal chance of being selected. The sample represents a smaller and more manageable portion of the people that can be studied and analyzed. It's a fundamental technique to gather data and make inferences about a population. Random sampling is considered a fair and unbiased sample selection method. This type of sampling is the most straightforward sample selection method.

The sample() function in R allows you to randomly sample from a specified set of (scalar) objects. You can also use the sample() function to sample rows from a data frame or a list.

Examples of how to use the sample() function:

  • To randomly sample 4 numbers from 1 to 10, you would use the following code:

sample(1:10, 4)

  • To randomly sample 5 letters from the alphabet, you would use the following code:

sample(letters, 5)

  • To randomly permute the numbers from 1 to 10, you would use the following code:

sample(1:10)

To randomly sample 5 rows from the airquality data frame, you would use the following code:

library(datasets)

data(airquality)


set.seed(20)


idx <- seq_len(nrow(airquality))

samp <- sample(idx, 5)


airquality[samp, ]

Post a Comment

0Comments
Post a Comment (0)