![]() |
VOOZH | about |
In this article, we will discuss how to generate a sample using the sample function in R.
Sample() function is used to generate the random elements from the given data with or without replacement.
Syntax:
sample(data, size, replace = FALSE, prob = NULL)
where,
Here, we will generate the n sample data from the given vector with 11 elements using the sample function.
Output:
[1] 45 7 5 34 [1] 3 [1] 5 23 8 21 6 45
Here we are going to create a vector with 11 elements and generate the sample data with a replacement.
Output:
[1] 45 5 5 3 [1] 86 [1] 5 5 8 7 8 45
Here we are going to select the elements with higher probability than others by setting the probability using the prob parameter.
Output:
[1] 23 23 23 23 23 45 23 23 23 23
Here we are going to sample the dataframe, let us create a dataframe and sample the rows.
Output:
👁 ImageHere we are going to sample the data in the list with size 4
Output:
[[1]] [1] 2 [[2]] [1] 1 [[3]] [1] 4 [[4]] [1] 6