From p@kr|v|t@ky @end|ng |rom un@w@edu@@u  Tue Mar  7 11:49:41 2023
From: p@kr|v|t@ky @end|ng |rom un@w@edu@@u (Pavel Krivitsky)
Date: Tue, 7 Mar 2023 10:49:41 +0000
Subject: [Rd] Augment base::replace(x, list,
 value) to allow list= to be a predicate?
In-Reply-To: <60f31223-033a-21ee-67fd-b1a6566a0278@insa-toulouse.fr>
References: <c450f50c118c8e4ca10acaf7b1fc40e7b205be57.camel@unsw.edu.au>
 <60f31223-033a-21ee-67fd-b1a6566a0278@insa-toulouse.fr>
Message-ID: <d9dc22049a09fdbf55b43b21a947f61dda3d0720.camel@unsw.edu.au>

Dear Serguei,

On Mon, 2023-03-06 at 09:45 +0100, Serguei Sokol wrote:
> Right, but anonymous function syntax can palliate to this:
> 
> x |> (\(x) replace(x, is.na(x), 0))()

This approach hardly makes for concise or readable code.

> 
> Before modifying the base of R, we should examine existing
> possibilities to achieve the same goal.

Backwards-compatible enhancements to existing functions in base R
appear in every major release. In this case, the proposed behaviour of
replace() would be consistent with that of the *apply() family of
functions.

> In this particular case and if the previous solution (anonymous 
> function) is not satisfactory a thin one-line wrapper can make the
> job:
> 
> freplace <- function (x, list, values, ...) replace(x, 
> if(is.function(list)) list <- list(x, ...) else list, values)
> 
> this becomes
> 
> c(1,2,NA,3) |> freplace(is.na, 0)
> 
> and looks quite acceptable for me.

One could always implement a function that has this functionality, of
course. In fact, one could write,

replace <- function (x, list, values, ...) base::replace(x, 
  if(is.function(list)) list <- list(x, ...) else list, values)

and not even have to change the name. However, having to write it every
time defeats the purpose of having it in base in the first place.

				Best Regards,
				Pavel

> 
> Best,
> Serguei.
> 
> > 
> > ????????????????????????Any thoughts?
> > ????????????????????????Pavel
> > ______________________________________________
> > R-devel at r-project.org?mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



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