Search code examples
rdataframelogical-operatorsany

Why is any() only defined for a numeric and not logical data.frame?


This appears to be quite surprising:

df1 <- data.frame(A=TRUE, B=FALSE)
df2 <- data.frame(A=1, B=2)

> any(df1)
Error in FUN(X[[i]], ...) :
  only defined on a data frame with all numeric variables

> any(df2)
[1] TRUE

This doesn't seem to be a bug because the error correctly states that any() will only work in the case where all variables within a data.frame are numeric.

But what is the reason for any() to work on all numeric variables and not when values are all logical?


Solution

  • Not sure when this was changed, but in R version 4.2.3 this works without issues:

    df1 <- data.frame(A=TRUE, B=FALSE)
    df2 <- data.frame(A=1, B=2)
    
    > any(df1)
    [1] TRUE
    
    > any(df2)
    [1] TRUE