search for: freplace

Displaying 2 results from an estimated 2 matches for "freplace".

Did you mean: replace
2023 Mar 06
1
Augment base::replace(x, list, value) to allow list= to be a predicate?
...;- list(x, ...) > x[list] <- values > x > } Before modifying the base of R, we should examine existing possibilities to achieve the same goal. 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) > > Then, the following is possible: > > c(1,2,NA,3) |> replace(is.na, 0) this becomes c(1,2,NA,3) |> freplace(is.na, 0) and looks quite acceptable for me. Best,...
2023 Mar 04
3
Augment base::replace(x, list, value) to allow list= to be a predicate?
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