Anwar Hossain Anwar Hossain -4 years ago 103
R Question

sample percentage of rows in dataframe for 1000 times with identificaton for each sampling

Related to this question.

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

But I want this random sampling for 1000 times with identification number.

For example, from these 1000 random sample I want to call the 5th or 70th sample. How can I do this?

Answer Source

We can do replicate to sample it 1000 times to store it in a list

lst <- replicate(1000, mydf[sample(nrow(mydf), round(0.25*nrow(mydf))),] , simplify = FALSE)

Then, get the 5th and 70th list element by subsetting with index

lst[c(5, 70)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download