Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export(collapseDim)
export(collapseNames)
export(commonYears)
export(complete_magpie)
export(condReplace)
export(convergence)
export(copy.attributes)
export(copy.magpie)
Expand Down
37 changes: 37 additions & 0 deletions R/condReplace.R
Original file line number Diff line number Diff line change
@@ -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)
}
Comment on lines +26 to +37
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following removes the redundant applications of the condition function and the conversions to logical. Also pulls out the actual replacement.

Suggested change
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)
}
condReplace <- function(x, condition, replace) {
if (is.function(condition)) {
replace <- magpie_expand(x = as.magpie(replace), ref = x)
condition <- as.logical(condition(x))
} else {
replace <- magpie_expand(x = as.magpie(replace), ref = x)
condition <- as.logical(magpie_expand(x = as.magpie(condition), ref = x))
}
x[condition] <- replace[condition]
return(x)
}

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -56,7 +56,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **magclass** in publications use:

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 <https://doi.org/10.5281/zenodo.1158580>, Version: 7.4.2, <https://github.com/pik-piam/magclass>.
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 <https://doi.org/10.5281/zenodo.1158580>, Version: 7.5.0, <https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
```
1 change: 1 addition & 0 deletions magclass.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: e2f34f9f-4198-499c-9065-cd23d47d5efd

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
1 change: 1 addition & 0 deletions man/magpply.Rd

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

1 change: 1 addition & 0 deletions man/mcalc.Rd

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

1 change: 1 addition & 0 deletions man/mselect.Rd

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

1 change: 1 addition & 0 deletions man/where.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/test-condReplace.R
Original file line number Diff line number Diff line change
@@ -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))
})
Loading