Skip to content

Commit

Permalink
Summary.data.frame() and Math.data.frame() now do accept logical colu…
Browse files Browse the repository at this point in the history
…mns as "numeric-alike"

git-svn-id: https://svn.r-project.org/R/trunk@79353 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Oct 20, 2020
1 parent a33b3ac commit 133f2a9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion doc/NEWS.Rd
Expand Up @@ -330,7 +330,13 @@
including two patch proposals by Brodie Gaslam.
\item \code{qnorm(<very large negative>, log.p=TRUE)} is now correct
to at least five digits where it was catastrophically wrong, previously.
to at least five digits where it was catastrophically wrong,
previously.
\item \code{sum(df)} and similar \code{"Summary"}- and
\code{"Math"}-group member functions now work for data frames
\code{df} with \code{\link{logical}} columns, notably also of zero
rows. Reported to R-devel by Martin "b706".
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/library/base/R/dataframe.R
Expand Up @@ -1606,14 +1606,15 @@ as.matrix.data.frame <- function (x, rownames.force = NA, ...)

Math.data.frame <- function (x, ...)
{
mode.ok <- vapply(x, function(x) is.numeric(x) || is.complex(x), NA)
mode.ok <- vapply(x, function(x)
is.numeric(x) || is.logical(x) || is.complex(x), NA)
if (all(mode.ok)) {
x[] <- lapply(X = x, FUN = .Generic, ...)
return(x)
} else {
vnames <- names(x)
if (is.null(vnames)) vnames <- seq_along(x)
stop("non-numeric variable(s) in data frame: ",
stop("non-numeric-alike variable(s) in data frame: ",
paste(vnames[!mode.ok], collapse = ", "))
}
}
Expand Down Expand Up @@ -1699,8 +1700,8 @@ Summary.data.frame <- function(..., na.rm)
args <- list(...)
args <- lapply(args, function(x) {
x <- as.matrix(x)
if(!is.numeric(x) && !is.complex(x))
stop("only defined on a data frame with all numeric variables")
if(!is.numeric(x) && !is.logical(x) && !is.complex(x))
stop("only defined on a data frame with all numeric-alike variables")
x
})
do.call(.Generic, c(args, na.rm=na.rm))
Expand Down
7 changes: 6 additions & 1 deletion src/library/base/man/groupGeneric.Rd
@@ -1,6 +1,6 @@
% File src/library/base/man/groupGeneric.Rd
% Part of the R package, https://www.R-project.org
% Copyright 1995-2017 R Core Team
% Copyright 1995-2020 R Core Team
% Distributed under GPL 2 or later

\name{groupGeneric}
Expand Down Expand Up @@ -139,6 +139,11 @@
}
Members of this group dispatch on the first argument supplied.

Note that the \code{\link{data.frame}} methods for the
\code{"Summary"} and \code{"Math"} groups require \dQuote{numeric-alike}
columns \code{x}, i.e., fulfilling \preformatted{
is.numeric(x) || is.logical(x) || is.complex(x)}

\item Group \code{"Complex"}:
\itemize{
\item \code{Arg}, \code{Conj}, \code{Im}, \code{Mod}, \code{Re}
Expand Down
18 changes: 18 additions & 0 deletions tests/reg-tests-1d.R
Expand Up @@ -4553,6 +4553,24 @@ options(op)
## had worked up to R 3.6.3, but not from 4.0.0 to 4.0.3


## Summary() and Math() data.frame methods with *logical* columns
a <- na.omit(airquality)
aF <- a[FALSE,] # 0-row version of it
dL0 <- data.frame(x=numeric(), L=logical()) # logical column
stopifnot(exprs = {
## "Summary" :
sum(aF) == 0 # gave Error "only defined on a data frame with all numeric variables"
sum(subset(a, Ozone > 200)) == 0 # (ditto)
suppressWarnings(range(dL0) == c(Inf, -Inf)) # (2 warnings)
## "Math" , gave Error..: non-numeric variable(s) in data frame :
identical(exp(data.frame(L=TRUE)), data.frame(L=exp(TRUE)))
identical(sinL0 <- sin(dL0), data.frame(x=numeric(), L=numeric()))
identical(sinL0, log1p(dL0))
identical(cumsum(dL0), data.frame(x=numeric(), L=integer()))
})
## probably never worked in any R <= 4.0.3



## keep at end
rbind(last = proc.time() - .pt,
Expand Down

0 comments on commit 133f2a9

Please sign in to comment.