Search code examples
rprogramming-languagessimulationmontecarlo

R programming (Monte Carlo Simulation)


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


Solution

  • We can use sample_n from 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 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))