From p@kr|v|t@ky @end|ng |rom un@w@edu@@u  Sat Mar  4 01:21:48 2023
From: p@kr|v|t@ky @end|ng |rom un@w@edu@@u (Pavel Krivitsky)
Date: Sat, 4 Mar 2023 00:21:48 +0000
Subject: [Rd] Augment base::replace(x, list,
 value) to allow list= to be a predicate?
Message-ID: <c450f50c118c8e4ca10acaf7b1fc40e7b205be57.camel@unsw.edu.au>

Dear All,

Currently, list= in base::replace(x, list, value) has to be an index
vector. For me, at least, the most common use case is for list= to be
some simple property of elements of x, e.g.,

x <- c(1,2,NA,3)
replace(x, is.na(x), 0)

Particularly when using R pipes, which don't allow multiple
substitutions, it would simplify many of such cases if list= could be a
function that returns an index, e.g.,

replace <- function (x, list, values, ...) {
  # Here, list() refers to the argument, not the built-in.
  if(is.function(list)) list <- list(x, ...)
  x[list] <- values
  x
}

Then, the following is possible:

c(1,2,NA,3) |> replace(is.na, 0)

			Any thoughts?
			Pavel


[Rd] Augment base::replace(x, list, value) to allow list= to be a predicate?
Pavel Krivitsky