1
votes

I have a vector called "combined" with 1's and 0's

combined
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I sampled twice from this vector, each with a sample size of 3 and put it into a contingency table of counts as follows.

2    1
1    2

I want to reiterate this sampling 1000 times such that I end with 1000 contingency tables each with counts of 1s and 0s from the sampling.

This is what I tried:

sample1 = as.vector(replicate(10000, sample(combined, 3)))
sample2 = as.vector(replicate(10000, sample(combined, 3)))
con_table = table(sample1,sample2)

but I ended up only getting 1 table instead of 10000. Hoping to get some help.

    8109 7573
    7306 7012
1
If i'm understanding correctly, use replicate around the whole expression - replicate(5, table(sample(combined,3), sample(combined,3)) )thelatemail
Perfect, just what I was looking for, thank you! How would I turn all of these tables into a 2x2 matrix? I am trying to perform a fisher test on each onejohn.doe
Probably making combined a factor first so each level is always represented would work. combined <- as.factor(combined)thelatemail

1 Answers

0
votes

You need to wrap the entire expression, sample and table inside replicate. Add a conversion to a factor to ensure you always get a 2x2 table. E.g. a simple version with 2 replications:

combined <- rep(0:1,each=10)
combined <- as.factor(combined)
replicate(2, table(sample(combined,3), sample(combined,3)), simplify=FALSE)
#[[1]]
#   
#    0 1
#  0 0 1
#  1 1 1
#
#[[2]]
#   
#    0 1
#  0 1 1
#  1 0 1