Skip to content

Commit ecdcc00

Browse files
committed
Implemented replace(), a drop-in replacement for base::replace() that allows list= and values= to be functions.
1 parent 5f3fd93 commit ecdcc00

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: statnet.common
2-
Version: 4.12.0-483
3-
Date: 2025-05-19
2+
Version: 4.12.0-487
3+
Date: 2025-05-29
44
Title: Common R Scripts and Utilities Used by the Statnet Project Software
55
Authors@R: c(
66
person(c("Pavel", "N."), "Krivitsky", role=c("aut","cre"), email="pavel@statnet.org", comment=c(ORCID="0000-0002-9101-3362", affiliation="University of New South Wales")),
@@ -19,6 +19,7 @@ Encoding: UTF-8
1919
Suggests: covr,
2020
roxygen2,
2121
rlang (>= 1.1.1),
22+
purrr,
2223
MASS
2324
Remotes:
2425
krivit/roxygen2@anchor-sometimes

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export(persistEval)
121121
export(persistEvalQ)
122122
export(qrsolve)
123123
export(qrssolve)
124+
export(replace)
124125
export(rowweights)
125126
export(sandwich_ginv)
126127
export(sandwich_qrsolve)

R/misc.utilities.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,3 +1199,57 @@ modify_in_place <- function(x, value = x){
11991199

12001200
invisible(value) # Return invisibly.
12011201
}
1202+
1203+
#' Replace values in a vector according to functions
1204+
#'
1205+
#' This is a thin wrapper around [base::replace()] that allows `list`
1206+
#' and/or `values` to be functions that are evaluated on `x` to obtain
1207+
#' the replacement indices and values.
1208+
#'
1209+
#' `list` function is passed the whole vector `x` at once (not
1210+
#' elementwise) and any additional arguments to `replace()`, and must
1211+
#' return an indexing vector (numeric, logical, character,
1212+
#' etc.). `values` function is passed `x` after subsetting it by the
1213+
#' result of calling `list()`.
1214+
#'
1215+
#' If passing named arguments, `x`, `list`, and `values` may cause a
1216+
#' conflict.
1217+
#'
1218+
#' @param x a vector.
1219+
#' @param list either an index vector or a function (*not* a function
1220+
#' name).
1221+
#' @param values either a vector of replacement values or a function
1222+
#' (*not* a function name).
1223+
#' @param ... additional arguments to `list` if it is a function;
1224+
#' otherwise ignored.
1225+
#'
1226+
#' @return A vector with the values replaced.
1227+
#'
1228+
#' @seealso [purrr::modify()] family of functions.
1229+
#'
1230+
#' @examples
1231+
#'
1232+
#' (x <- rnorm(10))
1233+
#'
1234+
#' ### Replace elements of x that are < 1/4 with 0.
1235+
#'
1236+
#' # Note that this code is pipeable.
1237+
#' x |> replace(`<`, 0, 1/4)
1238+
#' # More readable, using lambda notation.
1239+
#' x |> replace(\(.x) .x < 1/4, 0)
1240+
#' # base equivalent.
1241+
#' stopifnot(identical(replace(x, `<`, 0, 1/4),
1242+
#' base::replace(x, x < 1/4, 0)))
1243+
#'
1244+
#' ### Multiply negative elements of x by 1i.
1245+
#'
1246+
#' x |> replace(\(.x) .x < 0, \(.x) .x * 1i)
1247+
#' stopifnot(identical(replace(x, \(.x) .x < 0, \(.x) .x * 1i),
1248+
#' base::replace(x, x < 0, x[x < 0] * 1i)))
1249+
#'
1250+
#' @export
1251+
replace <- function(x, list, values, ...) {
1252+
if (is.function(list)) list <- list(x, ...)
1253+
if (is.function(values)) values <- values(x[list], ...)
1254+
base::replace(x, list, values)
1255+
}

man/replace.Rd

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)