[R] apply( )

Petr Klasterecky klaster at karlin.mff.cuni.cz
Thu May 10 18:03:44 CEST 2007


ifelse(((x < y) | (x > z)), 1, 0)

Note in particular the use of | instead of || for elementwise comparisons.

Petr

Greg Tarpinian napsal(a):
> I have a question that must have a simple answer (but eludes me).
> I need a row-by-row logical comparison across three numeric variables
> in
> a data frame: foo$x, foo$y, foo$z.  The logic is
> 
>    if( x < y || x > z ) 1 else 0
> 
> for a particular row.
> 
> It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
> loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
> or some other more efficient method?
> 
> Thank you in advance,
> 
>     Greg
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list