[R] Creating two groups of random numbers

Boris Steipe boris.steipe at utoronto.ca
Thu Jun 29 17:09:04 CEST 2017


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



> On Jun 29, 2017, at 9:44 AM, Naike Wang <wangnaike1989 at gmail.com> wrote:
> 
> 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
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list