[R] apply if else statement to vector

rl at openmailbox.org rl at openmailbox.org
Thu Oct 2 11:01:57 CEST 2014


Subscribers,

What is the correct syntax to apply the 'if else' conditional statement 
to vector objects?

Example:

vectorx<-c(50,50,20,70)
vectory<-c(50,50,20,20)
vectorz<-function () {
	if (vectorx>vectory)
	vectorx
	else vectorx<-0
}

vectorz()
Warning message:
In if (vectorx > vectory) vectorx else vectorx <- 0 :
   the condition has length > 1 and only the first element will be used

The help manual (?'if') explains that only length=0 is acceptable; what 
is an appropriate alternative function to use please?

The desired result for vectorz is:

0 0 0 70



More information about the R-help mailing list