Cenoc
Cenoc

Reputation: 11662

OR (or any logical operator) together many columns in R

As explained above, I would like to OR multiple columns and was hoping to do so without having to explicitly reference the columns (d$a | d$b | d$c), but rather with an array of column names (c(a,b,c))

Upvotes: 3

Views: 574

Answers (2)

effel
effel

Reputation: 1421

As I understand it you want to take | rowwise.

d = data.frame(a = c(TRUE, FALSE), b = c(FALSE, FALSE))
#       a     b
# 1  TRUE FALSE
# 2 FALSE FALSE

So the expected result here is c(TRUE, FALSE).

You can use any over all columns in d:

apply(d, 1, any)
# [1]  TRUE FALSE

Over named columns in d:

apply(d[c("a", "b")], 1, any)
# [1]  TRUE FALSE

Performance considerations:

library(microbenchmark)
df <- data.frame(a = c(T, F, T, T), b = c(T, T, F, T), c = c(F, F, T, T))
microbenchmark(
  Reduce = Reduce(`|`, df),
  apply = apply(df, 1, any))
# Unit: microseconds
#    expr    min      lq     mean  median       uq     max neval cld
#  Reduce  6.597  8.2275 11.83880 10.0730  13.4080  36.246   100  a 
#   apply 56.423 59.9425 79.86748 66.8265 105.7645 178.409   100   b

Upvotes: 1

Julius Vainora
Julius Vainora

Reputation: 48211

(df <- data.frame(a = c(T, F, T, T), b = c(T, T, F, T), c = c(F, F, T, T)))
#       a     b     c
# 1  TRUE  TRUE FALSE
# 2 FALSE  TRUE FALSE
# 3  TRUE FALSE  TRUE
# 4  TRUE  TRUE  TRUE
Reduce(`|`, df)
# [1] TRUE TRUE TRUE TRUE
Reduce(`&`, df)
# [1] FALSE FALSE FALSE  TRUE

Upvotes: 3

Related Questions