I am looking to run a loop that selects 2 rows from a data frame 1000 times > store that into an object > then run the same loop that selects 3 rows 1000 times > store to object > then 4, and so on. The end goal is to compare the means/variances of the columns when different numbers of rows are randomly selected in the loop.
Code
iris<- iris
results2<- list()
counter<- 0
for (i in 1:1000) {
# sample 2 randomly selected rows 1000 times
tempsample2<- iris[sample(1:nrow(iris), 2, replace=F), ]
# store results of sampling into the created list
results2[[i]]= tempsample2
counter<- counter 1
print(counter)
}
I can manually store all of these loops into separate objects to then make comparisons, but I'm guessing that there is an easier way to do that. Can anyone help me with this? Thanks for your time.
CodePudding user response:
You can use lapply
and save the output as nested list.
select_rows <- 2:4
n_times <- 5
inds <- nrow(iris)
result <- lapply(select_rows, function(x)
replicate(n_times, iris[sample(inds, x), ], simplify = FALSE))
Change select_rows
and n_times
as per your choice.