Reputation: 13792
I'm trying to repeatedly sample two vectors, x
and y
, 100 times and compute a t-test after each sample. I then want to combine the results into a dataframe using the broom
package. My attempt at doing this is below. However, all I've managed to do is carry out 100 t-tests on the same two vectors.
library(plyr); library(broom)
x <- rnorm(10000, 3, 3)
y <- rnorm(10000, 5, 3)
x_sample <- sample(x, size = 20, replace = FALSE)
y_sample <- sample(y, size = 20, replace = FALSE)
ldply(1:100, function(x) tidy(t.test(x_sample, y_sample)))
Upvotes: 2
Views: 658
Reputation: 886938
We can use replicate
with mapply
. We replicate the sample
for n
times for 'x' and 'y', and do the t.test
for the corresponding list
elements using mapply
and then tidy
the output.
n <- 100
mapply(function(x,y) tidy(t.test(x,y)),
replicate(n, sample(x, size=20, replace=FALSE), simplify=FALSE),
replicate(n, sample(y, size=20, replace=FALSE), simplify=FALSE))
Upvotes: 1