Question

I have a dataframe on which a logical test is applied: is any of the columns TRUE?

x=data.frame(label=c('a','b','c'),outcome1=c(TRUE,FALSE,FALSE),outcome2=c(FALSE,FALSE,TRUE),outcome3=c(TRUE,FALSE,FALSE))
x=transform(x,result=any(outcome1,outcome2,outcome3))

x
#   label outcome1 outcome2 outcome3 result
# 1     a     TRUE    FALSE     TRUE   TRUE
# 2     b    FALSE    FALSE    FALSE   TRUE
# 3     c    FALSE     TRUE    FALSE   TRUE

I don't understand why the result of row 2 is TRUE since not all the column terms are true.

any(FALSE,FALSE,FALSE)
FALSE

What is the correct way to apply "any" in this case?

Was it helpful?

Solution

Your code calculates

any(c(TRUE,FALSE,FALSE),c(FALSE,FALSE,TRUE),c(TRUE,FALSE,FALSE))

and recycles the result to fill the column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top