-
Notifications
You must be signed in to change notification settings - Fork 28
Added new function condReplace.R #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| 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 | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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)) | ||
| }) |
There was a problem hiding this comment.
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.