diff --git a/.buildlibrary b/.buildlibrary index 87cc6202..9fd0e74a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '15228808' +ValidationKey: '15397500' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 4a92ea85..3514f5ab 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'magclass: Data Class and Tools for Handling Spatial-Temporal Data' -version: 7.4.2 -date-released: '2026-03-12' +version: 7.5.0 +date-released: '2026-03-18' abstract: Data class for increased interoperability working with spatial-temporal data together with corresponding functions and methods (conversions, basic calculations and basic data manipulation). The class distinguishes between spatial, temporal diff --git a/DESCRIPTION b/DESCRIPTION index a2737ece..49efd19a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: magclass Title: Data Class and Tools for Handling Spatial-Temporal Data -Version: 7.4.2 -Date: 2026-03-12 +Version: 7.5.0 +Date: 2026-03-18 Authors@R: c( person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre"), comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431")), diff --git a/NAMESPACE b/NAMESPACE index 323be34a..3ebd01de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(collapseDim) export(collapseNames) export(commonYears) export(complete_magpie) +export(condReplace) export(convergence) export(copy.attributes) export(copy.magpie) diff --git a/R/condReplace.R b/R/condReplace.R new file mode 100644 index 00000000..bf81bf1e --- /dev/null +++ b/R/condReplace.R @@ -0,0 +1,37 @@ +#' condReplace +#' +#' This function allows to replace parts of a magpie object based on a condition. +#' +#' @param x MAgPIE object to be modified +#' @param condition Either a function that returns TRUE/FALSE or a magpie object +#' with TRUE/FALSE or 1/0 content. Can have lower dimensionality than x. +#' @param replace MAgPIE object that shall replace the content of x if condition +#' is TRUE. Can have lower dimensionality than x. +#' +#' @return The modified MAgPIE object +#' @author Benjamin Bodirsky +#' @examples +#' replaceSingle <- condReplace(population_magpie, population_magpie > 1000, 1000) +#' replaceRow <- condReplace(population_magpie, dimSums(population_magpie, dim = 1) < 7000, NA) +#' containsNAs <- replaceRow +#' replaceBasedOnFunction <- condReplace(containsNAs, is.na, 0) +#' replaceUsingMean <- condReplace(containsNAs, is.na, +#' magpply(X = containsNAs, FUN = mean, DIM = 2, na.rm = TRUE)) +#' +#' @family SelectionCalculation +#' @export + + + +condReplace <- function(x, condition, replace) { + + if (is.function(condition)) { + replace <- magpie_expand(x = as.magpie(replace), ref = x) + x[condition(x)] <- replace[condition(x)] + } else { + replace <- magpie_expand(x = as.magpie(replace), ref = x) + condition <- magpie_expand(x = as.magpie(condition), ref = x) + x[as.logical(condition)] <- replace[as.logical(condition)] + } + return(x) +} diff --git a/README.md b/README.md index 266b3f1a..bac87982 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Data Class and Tools for Handling Spatial-Temporal Data -R package **magclass**, version **7.4.2** +R package **magclass**, version **7.5.0** [![CRAN status](https://www.r-pkg.org/badges/version/magclass)](https://cran.r-project.org/package=magclass) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158580.svg)](https://doi.org/10.5281/zenodo.1158580) [![R build status](https://github.com/pik-piam/magclass/workflows/check/badge.svg)](https://github.com/pik-piam/magclass/actions) [![codecov](https://codecov.io/gh/pik-piam/magclass/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/magclass) [![r-universe](https://pik-piam.r-universe.dev/badges/magclass)](https://pik-piam.r-universe.dev/builds) @@ -56,7 +56,7 @@ In case of questions / problems please contact Jan Philipp Dietrich , Version: 7.4.2, . +Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D, Sauer P, Baumstark L, Bertram C, Giannousakis A, Klein D, Neher I, Pehl M, Schultes A, Stevanovic M, Wang X, Beier F, Pflüger M, Richters O, Rein P (2026). "magclass: Data Class and Tools for Handling Spatial-Temporal Data." doi:10.5281/zenodo.1158580 , Version: 7.5.0, . A BibTeX entry for LaTeX users is @@ -65,9 +65,9 @@ A BibTeX entry for LaTeX users is title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data}, author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip and Pascal Sauer and Lavinia Baumstark and Christoph Bertram and Anastasis Giannousakis and David Klein and Ina Neher and Michaja Pehl and Anselm Schultes and Miodrag Stevanovic and Xiaoxi Wang and Felicitas Beier and Mika Pflüger and Oliver Richters and Patrick Rein}, doi = {10.5281/zenodo.1158580}, - date = {2026-03-12}, + date = {2026-03-18}, year = {2026}, url = {https://github.com/pik-piam/magclass}, - note = {Version: 7.4.2}, + note = {Version: 7.5.0}, } ``` diff --git a/magclass.Rproj b/magclass.Rproj index e5c3f7cb..ed52c973 100644 --- a/magclass.Rproj +++ b/magclass.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: e2f34f9f-4198-499c-9065-cd23d47d5efd RestoreWorkspace: Default SaveWorkspace: Default diff --git a/man/magpply.Rd b/man/magpply.Rd index c07b0531..1da41e52 100644 --- a/man/magpply.Rd +++ b/man/magpply.Rd @@ -36,6 +36,7 @@ magpply(fourdim, FUN = sum, MARGIN = c(1, 3.1)) } \seealso{ Other SelectionCalculation: +\code{\link{condReplace}()}, \code{\link{mcalc}()}, \code{\link{mselect}()}, \code{\link{where}()} diff --git a/man/mcalc.Rd b/man/mcalc.Rd index a1fe1b0e..1305b587 100644 --- a/man/mcalc.Rd +++ b/man/mcalc.Rd @@ -42,6 +42,7 @@ mcalc(pop, `Nearly B1` ~ 0.5 * A2 + 99.5 * B1) \code{\link{mselect}} Other SelectionCalculation: +\code{\link{condReplace}()}, \code{\link{magpply}()}, \code{\link{mselect}()}, \code{\link{where}()} diff --git a/man/mselect.Rd b/man/mselect.Rd index 35e5cd7e..5827570e 100644 --- a/man/mselect.Rd +++ b/man/mselect.Rd @@ -47,6 +47,7 @@ mselect(pop, i = c("AFR", "EUR"), scenario = "A2", t = "y2035") \code{\link{collapseNames}}, \code{"\linkS4class{magpie}"} Other SelectionCalculation: +\code{\link{condReplace}()}, \code{\link{magpply}()}, \code{\link{mcalc}()}, \code{\link{where}()} diff --git a/man/where.Rd b/man/where.Rd index 2f9a3618..c796061b 100644 --- a/man/where.Rd +++ b/man/where.Rd @@ -26,6 +26,7 @@ where(p > 500) whereplot in package luplot Other SelectionCalculation: +\code{\link{condReplace}()}, \code{\link{magpply}()}, \code{\link{mcalc}()}, \code{\link{mselect}()} diff --git a/tests/testthat/test-condReplace.R b/tests/testthat/test-condReplace.R new file mode 100644 index 00000000..782c2d7c --- /dev/null +++ b/tests/testthat/test-condReplace.R @@ -0,0 +1,33 @@ +test_that("complete_magpie works", { + m <- new.magpie(c("A", "B"), 2000, c("scen1", "scen2"), fill = rep(100, 4)) + + # Base cases + m2 <- m + m2["A", , "scen1"] <- 2000 + replacedM <- condReplace(m2, m2 > 1000, 1000) + expect_true(all(replacedM["A", , "scen1"] == 1000)) + + m2 <- m + m2["A", , "scen1"] <- NA + replacedM <- condReplace(m2, is.na, 1000) + expect_true(all(replacedM["A", , "scen1"] == 1000)) + + # Expanding a magpie condition + replacedM <- condReplace(m, new.magpie("B", fill = TRUE), 2) + expect_true(all(replacedM["B", , ] == 2)) + + # Expanding a magpie replacement with existing values + m2 <- m + m2["A", , "scen1"] <- 2000 + m2["B", , "scen1"] <- 2000 + replacedM <- condReplace(m, m2 > 1000, new.magpie("A", "B", fill = 2)) + expect_true(all(replacedM[, , "scen1"] == 2)) + + # Expanding a magpie replacement with missing values + m2 <- m + m2["A", , "scen1"] <- 2000 + m2["B", , "scen2"] <- 2000 + replacedM <- condReplace(m, m2 > 1000, new.magpie("A", fill = 2)) + expect_true(all(replacedM["A", , "scen1"] == 2)) + expect_true(all(replacedM["B", , "scen2"] == 2)) +}) \ No newline at end of file