Skip to content

Commit 2183dc5

Browse files
committed
It is now possible to assign to replace().
1 parent 979732e commit 2183dc5

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: statnet.common
2-
Version: 4.12.0-488
2+
Version: 4.12.0-491
33
Date: 2025-05-29
44
Title: Common R Scripts and Utilities Used by the Statnet Project Software
55
Authors@R: c(

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ S3method(update,Welford)
4949
export("EVL<-")
5050
export("NVL<-")
5151
export("lrowweights<-")
52+
export("replace<-")
5253
export("rowweights<-")
5354
export("ult<-")
5455
export(.Deprecate_method)

R/misc.utilities.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,12 +1204,13 @@ modify_in_place <- function(x, value = x){
12041204
#'
12051205
#' This is a thin wrapper around [base::replace()] that allows `list`
12061206
#' and/or `values` to be functions that are evaluated on `x` to obtain
1207-
#' the replacement indices and values.
1207+
#' the replacement indices and values. The assignment version replaces
1208+
#' `x`.
12081209
#'
12091210
#' `list` function is passed the whole vector `x` at once (not
12101211
#' elementwise) and any additional arguments to `replace()`, and must
12111212
#' return an indexing vector (numeric, logical, character,
1212-
#' etc.). `values` function is passed `x` after subsetting it by the
1213+
#' etc.). `values`/`value` function is passed `x` after subsetting it by the
12131214
#' result of calling `list()`.
12141215
#'
12151216
#' If passing named arguments, `x`, `list`, and `values` may cause a
@@ -1218,7 +1219,7 @@ modify_in_place <- function(x, value = x){
12181219
#' @param x a vector.
12191220
#' @param list either an index vector or a function (*not* a function
12201221
#' name).
1221-
#' @param values either a vector of replacement values or a function
1222+
#' @param values,value either a vector of replacement values or a function
12221223
#' (*not* a function name).
12231224
#' @param ... additional arguments to `list` if it is a function;
12241225
#' otherwise ignored.
@@ -1247,9 +1248,20 @@ modify_in_place <- function(x, value = x){
12471248
#' stopifnot(identical(replace(x, \(.x) .x < 0, \(.x) .x * 1i),
12481249
#' base::replace(x, x < 0, x[x < 0] * 1i)))
12491250
#'
1251+
#' ### Modify the list in place.
1252+
#'
1253+
#' y <- x
1254+
#' replace(x, `<`, 1/4) <- 0
1255+
#' x
1256+
#' stopifnot(identical(x, replace(y, `<`, 0, 1/4)))
1257+
#'
12501258
#' @export
12511259
replace <- function(x, list, values, ...) {
12521260
if (is.function(list)) list <- list(x, ...)
12531261
if (is.function(values)) values <- values(x[list], ...)
12541262
base::replace(x, list, values)
12551263
}
1264+
1265+
#' @rdname replace
1266+
#' @export
1267+
`replace<-` <- function(x, list, ..., value) replace(x, list, value, ...)

man/replace.Rd

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

0 commit comments

Comments
 (0)