John Smith
John Smith

Reputation:

I am unable to create this variable

How do I create a variable in R, which takes a value of 0 60% of the time and a value of 1 otherwise?

Upvotes: 1

Views: 45

Answers (2)

CFD
CFD

Reputation: 198

R offers a series of functions that produce random samples from common distributions. They are all named "r" followed by an abbreviation of the distribution; "rnorm" - random normal, "rpois" - random poisson, "runif" - random uniform (not run-if), etc. You're looking for "rbinom" - random binomial.

e.g.

rbinom(1, 1, 0.4)

We use 0.4 instead of 0.6 because the function wants the probability of a 1 as the third argument.

Upvotes: 2

alan ocallaghan
alan ocallaghan

Reputation: 3038

This should probably be on StackOverflow. Anyway,

sample(0:1, 1, prob=c(0.6, 0.4))

Upvotes: 1

Related Questions