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

Karolis Koncevičius k@ro||@@koncev|c|u@ @end|ng |rom gm@||@com
Sun Feb 16 20:23:47 CET 2020


I recently stumbled on an unusual behaviour of any() and all() and have 
been adviced from StackOverflow to share it here [1].

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

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

     > any(df2)
     [1] TRUE
     Warning message: In any(c(1, 2), na.rm = FALSE):
       coercing argument of type 'double' to logical

Same results happen when using all()

So the any() and all() do not work on data frames with logical values, 
but work if the values are numeric.

This doesn't seem to be a bug because error correctly states that any() 
will only work on an all-numeric data.frame. However the behaviour 
doesn't seem intentional and I cannot come up with any reason for it 
behaving this way.

Maybe any() and all() need to be modified to not work on data.frames() 
at all, which would also be consistent with is.nan() ?

[1]: https://stackoverflow.com/questions/60251847/why-is-any-only-defined-for-a-numeric-and-not-logical-data-frame

More information about the R-devel mailing list