[R] apply( )

Greg Tarpinian sasprog474 at yahoo.com
Thu May 10 18:37:10 CEST 2007


Thank you all for your answers...  To summarize,

  1. with(X, ifelse(V1 < V2 | V1 > V3, 1, 0))
  2. ifelse((foo$x < foo$y) | (foo$x > foo$z), 1, 0)
  3. with(foo, (x < y) * (x > z))

were the responses to my question (see below).

Kindly,

    Greg


----------- Original post -----------
> 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



More information about the R-help mailing list