Sample() function in R, generates a sample of the specified size from the data set or elements, either with or without replacement. Sample() function is used to get the sample of a numeric and character vector and also dataframe. Lets see an example of
- sample of a numeric and character vector using sample() function in R
- sample of a dataframe using sample() function in R.
Syntax for Sample() Function in R:
|x||Data Set or a vector of one or more elements from which sample is to be chosen|
|size||size of a sample|
|replace||Should sampling be with replacement?|
|prob||probability weights for obtaining the elements of the vector being sampled|
Sample function in R with replacement:
Lets see an example that generates 10 random sample from vector of 1 to 20. With replacement =TRUE. which means value in the sample can occur more than once
## basic Sample function in R sample(1:20, 10, replace=TRUE)
When we run the above code the output will be
Sample Function in R with set.seed():
If we run the sample function again and again, it gives different set of samples each time.What if we require the same sample each time?
For that we use set.seed() function. Set.seed() function always returns the same pseudo-random sequence.
## Sample function in R with set.seed() set.seed(10) sample(1:20, 10, replace=TRUE)
Whenever we run the sample function along with the set.seed() function it outputs same set of samples.
so the output always will be the same for set.seed(10)
Sample function with no Replacement:
By passing an argument replace = FALSE. We can draw a sample without replacement
## Sample function in R with No Replacement sample(1:20, 10, replace=FALSE)
so the output will be without any repetition
Sample Function in R with dataset:
Let’s extract set of sample elements from the data set with the help of sample function in R. We will use default mtcars table in R.
## applying Sample function in R to mt cars table to extract 5 sample rows set.seed(123) index<-sample(1:nrow(mtcars), 5) index mtcars[index,]
when we execute the above code ·
- sample function returns the 5 row indexes to the index object·
- Then that 5 indexes are passed as input to the mtcars to fetch that 5 rows
So the output will be
and the sample dataframe will be
As we have set seeds it will give the same set of 5 rows as a sample.
Sample Function in R with dataset with replacement:
Let’s extract set of sample elements from the data set with replacement with the help of sample() function. We will use default iris table.
## applying Sample function in R with replacement set.seed(123) index = sample(1:nrow(iris), 10,replace = TRUE) index mtcars[index,]
as the result we will generate sample 10 rows from the iris dataframe using sample() function with replacement. so the resultant sample may have repeated rows as shown below