Simple Random Sampling in R : In Simple random sampling every individuals are randomly obtained and so the individuals are equally likely to be chosen. If a population has ‘n’ individuals all the individuals has equal probability of being chosen i.e. 1/n. Lets see in R.
Simple random sampling of dataframe in R:
sample() function is used to get the random sampling of dataframe in R as shown below. We are using iris dataset
# simple Random Sampling in R iris_df <- iris set.seed(1000) iris_df[sample(nrow(iris_df), 5),]
So the 5 randomly selected rows are
Simple random sampling of dataframe in R using dplyr:
dplyr package is used to get the random sampling of dataframe in R as shown below.
# simple Random Sampling in R dplyr library(dplyr) iris_df <- iris set.seed(1000) sample_n(iris_df, 10)
So the 10 randomly selected rows are
Simple random sampling of list in R
set.seed(1000) SRS_ARR <-sample(1:500, 20) SRS_ARR
The above code gets the simple random sampling of first 500 numbers
Output:
[1] 164 379 57 344 257 34 365 288 107 126 172 370 155 422 372 36 239 307 41 281