Skip to content
Prev 325647 / 398503 Next

alternative to ifelse

On Wed, 19 Jun 2013, Brian Perron <beperron at umich.edu> writes:
Hi Brian,

put all your x into a matrix and use apply:

  X <- cbind(x1, x2, x3, x4)
  apply(X, 1, function(x) if (any(x == 1L)) 1 else 0)

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

or, since TRUE and FALSE evaluate to 1 and 0 when coerced to numeric:

  as.integer(apply(X, 1, function(x) any(x == 1L)))

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



Regards,
        Enrico
Message-ID: <8761xa9d3b.fsf@enricoschumann.net>
In-Reply-To: <CADB2ZiTDKkAHjawnZi4pdfFZ1JYwWfQYUvUVs-nE2KZaBTUxDA@mail.gmail.com> (Brian Perron's message of "Wed, 19 Jun 2013 09:24:50 -0400")