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]]
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.
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:> 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. > > --- > This email has been checked for viruses by AVG. > http://www.avg.com > >-- Michael http://www.dewey.myzen.co.uk/home.html