[英]Is it possible to generate a sample of 10 from a population and repeat that sampling 1000 times?
[英]sample percentage of rows in dataframe for 1000 times with identificaton for each sampling
與這個問題有關。
gender <- c("F", "M", "M", "F", "F", "M", "F", "F")
age <- c(23, 25, 27, 29, 31, 33, 35, 37)
mydf <- data.frame(gender, age)
mydf[sample(nrow(mydf), round(0.25*nrow(mydf))),]
但我想要這個帶有識別號的隨機抽樣 1000 次。
例如,從這 1000 個隨機樣本中,我想調用第 5 個或第 70 個樣本。 我怎樣才能做到這一點?
我們可以進行replicate
以對其進行 1000 次采樣以將其存儲在list
lst <- replicate(1000, mydf[sample(nrow(mydf), round(0.25*nrow(mydf))),] , simplify = FALSE)
然后,通過索引子集得到第 5 個和第 70 個list
元素
lst[c(5, 70)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.