Hi there, I guess this is an easy one, but still: I would like to randomly sample 0s and 1s but in a way that I end up having for example 70% of 1s and the rest of 0s and not 50:50 as this function does: sample(c(0,1), 100, replace = TRUE) Any recommendations? Many thanks! Ana [[alternative HTML version deleted]]
Hi Ana, Look at the documentation for ?sample, specifically, the "prob" argument. In your case this should work: sample(c(0,1), 100, replace = TRUE, prob = c(.3, .7)) note that you may not have *exactly* 70% 1 and 30%, in any given sample. HTH, Josh On Tue, Jul 5, 2011 at 11:21 AM, Ana Kolar <annakolar at yahoo.com> wrote:> Hi there, > > I guess this is an easy one, but still: > > I would like to randomly sample 0s and 1s but in a way that I end up having for example 70% of 1s and the rest of 0s and not 50:50 as this function does:?sample(c(0,1), 100, replace = TRUE) > > Any recommendations? > > > Many thanks! > > Ana > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/
On 05/07/2011 2:25 PM, Joshua Wiley wrote:> Hi Ana, > > Look at the documentation for ?sample, specifically, the "prob" > argument. In your case this should work: > > sample(c(0,1), 100, replace = TRUE, prob = c(.3, .7)) > > note that you may not have *exactly* 70% 1 and 30%, in any given sample.And if you want exact counts, you can use sample to permute a vector. For example: sample(rep(0:1, c(30, 70))) Duncan Murdoch> HTH, > > Josh > > On Tue, Jul 5, 2011 at 11:21 AM, Ana Kolar<annakolar at yahoo.com> wrote: > > Hi there, > > > > I guess this is an easy one, but still: > > > > I would like to randomly sample 0s and 1s but in a way that I end up having for example 70% of 1s and the rest of 0s and not 50:50 as this function does: sample(c(0,1), 100, replace = TRUE) > > > > Any recommendations? > > > > > > Many thanks! > > > > Ana > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > R-help at r-project.org mailing list > > 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. > > > > > > >
Seemingly Similar Threads
- how to extract data from a function printout - example provided
- Calculating interval for conditional/unconditional correlation matrix
- Three dimensional view of the profiles using 'rgl' package (example of 3 dimensional graphics using rgl package).
- MatchIt Package
- extracting data