Karolis Koncevičius
2020-Feb-16 19:23 UTC
[Rd] Why is any() only defined for a numeric and not logical data.frame?
Hello, 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
Rui Barradas
2020-Feb-16 19:47 UTC
[Rd] Why is any() only defined for a numeric and not logical data.frame?
Hello, As it turns out, this is valid for all generics of the ?Summary group. From help("Summary"): Group "Summary": all, any sum, prod min, max range methods("Summary") shows that there is a method for df's. And the code of Summary.data.frame has an explicit test if (!is.numeric(x) && !is.complex(x)) stop("only defined on a data frame with all numeric variables") So now the question is even more pertinent (?)(I think): If sum(<logical vector>) is a valid instruction, why only sum(<numeric df>) sum(<complex df>) are valid? Rui Barradas ?s 19:23 de 16/02/20, Karolis Koncevi?ius escreveu:> Hello, > > 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 > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Maybe Matching Threads
- Should '@" now be listed in tools:::.get_internal_S3_generics() ?
- Should '@" now be listed in tools:::.get_internal_S3_generics() ?
- Should '@" now be listed in tools:::.get_internal_S3_generics() ?
- Should '@" now be listed in tools:::.get_internal_S3_generics() ?
- Should '@" now be listed in tools:::.get_internal_S3_generics() ?