Reputation: 23
Hi I would like to ask how to sample out 50 instances of iris data (which contain 150 instances) by using Monte Carlo Simulation ? Any idea? Many thanks
Upvotes: 1
Views: 1052
Reputation: 3619
Here is one way to do it in base R.
You can sample 50 rows with replacement with
iris[sample(1:nrow(iris), size = 50, replace = TRUE), ]
To make a list, e.g., of 1000 samples of 50 rows with replacement, you can use lapply
.
iris_mc_samps <- lapply(1:1000, function(x) iris[sample(1:nrow(iris), size = 50, replace = TRUE), ])
Upvotes: 1
Reputation: 39154
We can use sample_n
from dplyr to select 50 rows with replacement.
# Set seed for reproducibility
set.seed(12800)
library(dplyr)
library(purrr)
iris_sub <- iris %>% sample_n(size = 50, replace = TRUE)
And here I show one approach that can repeat this process for 1000 times, using map_dfr
from the purrr package. The end result is a data frame with 50000 rows. A new column called Time
is created to document the number of sampling.
iris_sample <- map_dfr(1:1000, ~iris %>%
sample_n(size = 50, replace = TRUE) %>%
mutate(Time = .x))
Upvotes: 3