Skip to content

Commit

Permalink
performance improvements in data.frame methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Dec 6, 2023
1 parent 36a5750 commit 875fdc6
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: quantities
Type: Package
Title: Quantity Calculus for R Vectors
Version: 0.2.1
Version: 0.2.1.1
Authors@R: c(
person("Iñaki", "Ucar", email="[email protected]",
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")))
Expand All @@ -22,6 +22,6 @@ Suggests: dplyr (>= 1.0.0), vctrs (>= 0.5.0), tidyr, pillar, ggplot2 (> 3.2.1),
testthat, vdiffr, knitr, rmarkdown
LinkingTo: Rcpp (>= 0.12.10)
ByteCompile: yes
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
Roxygen: list(old_usage = TRUE)
VignetteBuilder: knitr
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# quantities devel

- Performance improvements for `data.frame` methods.

# quantities 0.2.1

- Fix a couple of tests (r-quantities/units#324).
Expand Down
6 changes: 1 addition & 5 deletions R/drop.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,5 @@ drop_errors.quantities <- drop_units.quantities
#' @name drop_quantities
#' @export
drop_quantities.data.frame <- function(x) {
for (i in seq_along(x)) {
if (inherits(x[[i]], "quantities"))
x[[i]] <- drop_quantities(x[[i]])
}
x
dfapply(x, function(i) if (inherits(i, "quantities")) drop_quantities(i) else i)
}
14 changes: 2 additions & 12 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,8 @@ diff.quantities <- function(x, lag = 1L, differences = 1L, ...) reclass(NextMeth
#' as.data.frame(z)
#'
#' @export
as.data.frame.quantities <- function(x, row.names = NULL, optional = FALSE, ...) {
e <- errors(x)
dim(e) <- dim(x)
e <- as.data.frame(e)
value <- NextMethod()
if (!optional && ncol(value) == 1)
colnames(value) <- deparse(substitute(x))
for (i in seq_len(ncol(value))) {
value[[i]] <- reclass(value[[i]])
errors(value[[i]]) <- e[[i]]
}
value
as.data.frame.quantities <- function(x, ...) {
dfapply(as.data.frame(drop_units(x), ...), as_units, units(x))
}

#' Coerce to a List
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ reclass <- function(x) {
class(x) <- c("quantities", "units", "errors")
x
}

dfapply <- function(X, FUN, ...) {
attrs <- attributes(X)
X <- lapply(X, FUN, ...)
attributes(X) <- attrs
X
}
14 changes: 1 addition & 13 deletions man/as.data.frame.quantities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/cbind.quantities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 875fdc6

Please sign in to comment.