Skip to content

Creating two groups of random numbers

3 messages · Naike Wang, Boris Steipe, Michael Dewey

#
Hi all,
I want to create two groups of random numbers to calculate proportions. The
first group is to represent the number of cases in a study. The second
group is to represent the sample size of the study. Apparently, the sample
size is going to have to be bigger or equal to the number of cases, but the
sample size of a study is not necessarily greater than the number of cases
of another study. Here's an example:

study                 cases            total
1                        17                 28
2                        48                 70
3                        87                 92
4                        15                 17



Notice that the sample size of the first study is 28, which is bigger than
the number of cases of this study, but is smaller than the number of cases
of the second study.

How do I create a data set like this?

Best,
Naike
#
I'd do it this way ... let me know if you need explanations.

minSize <- 15
maxSize <- 100
minSample <- 0.1
maxSample <- 0.8

# setup dataframe with totals, and cases as fractions
myStudies <- data.frame(study = 1:Nstudies,
                        cases = runif(Nstudies,
                                      min = minSample,
                                      max = maxSample),
                        total = sample(minSize:maxSize,
                                       Nstudies,
                                       replace = TRUE))

# convert case fractions of totals to integers
myStudies$cases <- round(myStudies$cases * myStudies$total)


Cheers,
Boris
#
Please do not cross post as people waste time replying on one forum not 
knowing you have already received excellent advice on another.
On 29/06/2017 14:44, Naike Wang wrote: