Question

Whats the most effective solution for that problem? A sample of 100 "0" and "1".

sample(0:1, 100, replace=TRUE, prob=NULL)

A) 70% of all generated numbers should be "1".

B) 80% of all generated numbers should be "1".

Whats the argument/or library that allows such kind of distribution?

Was it helpful?

Solution

What about:

rbinom(1, n = 100, prob = .7)

OTHER TIPS

To get exactly x apples and y oranges, you can build such vector and sample it:

sample(c(rep("apple", x), rep("orange", y)))

In your case:

sample(c(rep(1, 70), rep(0, 100 - 70)))

From your comment, it seems like you might just be using prob incorrectly.

Consider the following:

set.seed(1); x <- sample(0:1, 100, replace=TRUE, prob=c(.3, .7)); table(x)
# x
#  0  1 
# 32 68 
set.seed(2); x <- sample(0:1, 100, replace=TRUE, prob=c(.3, .7)); table(x)
# x
#  0  1 
# 31 69 
set.seed(1); x <- sample(0:1, 100, replace=TRUE, prob=c(.2, .8)); table(x)
# x
#  0  1 
# 17 83 
set.seed(2); x <- sample(0:1, 100, replace=TRUE, prob=c(.2, .8)); table(x)
# x
#  0  1 
# 23 77 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top