简体   繁体   中英

I am unable to create this variable

如何在 R 中创建一个变量,它在 60% 的时间内取值为 0,否则取值为 1?

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.

eg

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.

This should probably be on StackOverflow. Anyway,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM