Don't put a function inside ifelse(). It will only be executed once and the value repeated. But rbinom() will accept a vector of probabilities so use ifelse() to create a vector of probabilities for rbinom():

> set.seed(42)

> x <- c(0, 1, 0, 1, 0)

# The next 3 will give different results each time, but

# all the 0 values will be the same and all the 1 values

# will be the same

> ifelse(x == 0, rbinom(1, 1, .5), rbinom(1, 1, .1))

[1] 1 1 1 1 1

> ifelse(x == 0, rbinom(1, 1, .5), rbinom(1, 1, .1))

[1] 0 0 0 0 0

> ifelse(x == 0, rbinom(1, 1, .5), rbinom(1, 1, .1))

[1] 1 0 1 0 1

> ifelse(x == 0, rbinom(1, 1, .5), rbinom(1, 1, .1))

[1] 1 0 1 0 1

# Now each value will vary

> prb <- ifelse(x == 0, .5, .1)

> rbinom(5, 1, prob=prb)

[1] 1 0 0 0 1

> rbinom(5, 1, prob=prb)

[1] 0 0 1 1 0

> rbinom(5, 1, prob=prb)

[1] 0 0 1 0 1

> rbinom(5, 1, prob=prb)

[1] 1 0 1 0 1

-------------------------------------

David L Carlson

Department of Anthropology

Texas A&M University

College Station, TX 77840-4352

From: Art U [mailto:

[hidden email]]

Sent: Wednesday, March 15, 2017 1:14 PM

To: David L Carlson <

[hidden email]>

Subject: Re: [R] Correlated variables

I have tried to use ifelse function, but its output in many runs gives vector of only zeros or ones, while usage of loops always provides diversity in vector.

On Wed, Mar 15, 2017 at 2:07 PM, David L Carlson <

[hidden email]> wrote:

You will have to use a for loop if you insist on using control statements such as if-else. You should really read up on the ifelse() function and vectorization in R:

> set.seed(42) # So your results will match these

> n <- 25

> x1 <- rnorm(n, 0, 1)

> x2 <- rnorm(n, 0, 1)

> x3 <- rnorm(n, 0, 1)

> prb <- ifelse(x1 + x2 - x3 > .25, .25, .5)

> prb

[1] 0.25 0.50 0.50 0.25 0.50 0.50 0.25 0.25 0.25 0.50 0.50 0.25 0.50 0.50

[15] 0.25 0.50 0.50 0.50 0.50 0.50 0.25 0.50 0.25 0.25 0.25

> t <- rbinom(n, 1, prob=prb)

> t

[1] 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0

?ifelse

?Control

?rbinom

-------------------------------------

David L Carlson

Department of Anthropology

Texas A&M University

College Station, TX 77840-4352

-----Original Message-----

From: R-help [mailto:

[hidden email]] On Behalf Of Art U

Sent: Wednesday, March 15, 2017 9:56 AM

To:

[hidden email]
Subject: [R] Correlated variables

Hi,

I'm trying to create binary variable which distribution conditioned on

other variables. That is what I did,

x1=rnorm(n,0,1);

x2=rnorm(n,0,1);

x3=rnorm(n,0,1);

if(x1+x2-x3>0.25){

t=rbinom(1, 1, prob=0.25)

}else{

t=rbinom(1, 1, prob=0.5)

}

But I always get this the warning:

Warning message:In if (x1 + x2 - x3 > 0.5) { :

the condition has length > 1 and only the first element will be used

Can I do this without using function "for"?

Thank you in advance.

Ariel

--

*I like to pretend I'm alone*. *Completely alone*. *Maybe post-apocalypse

or plague*... *Whatever*. *No-one left to act normal for. No need to hide

who I really am. It would be... freeing*. *...*

[[alternative HTML version deleted]]

______________________________________________

[hidden email] mailing list -- To UNSUBSCRIBE and more, see

https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide

http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.

--

I like to pretend I'm alone. Completely alone. Maybe post-apocalypse or plague... Whatever. No-one left to act normal for. No need to hide who I really am. It would be... freeing. ...

______________________________________________

[hidden email] mailing list -- To UNSUBSCRIBE and more, see

https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide

http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.