user6644063 user6644063 - 3 months ago 10
R Question

Create 1000 samples of 30 observations from data.frame

I have got a data.frame and I need to create 1000 random samples, each one containing 30 observations.

Here an example of my data.frame:

df = read.table(text = 'ID Letter
1 L
2 R
3 T
4 U
5 I
6 K
7 Y
8 Q
9 A
10 R', header = TRUE)


The column from where the samples must be taken is
df$Letter
.

Any suggestion?

Answer

You can use sample along with replicate to create 100 samples of length 30 with replacement. You do not need the boot package. Following your answer, you can just use simplify = FALSE to prevent coercing into an array.

replicate(100,sample(df$Letter, 30, replace =TRUE), simplify = FALSE)

The result will be a list.