简体   繁体   中英

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

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))

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, eg, 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), ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM