str_coalesce | R Documentation |
str_coalesce()
find the first non empty string ""
.
This is particularly useful for assigning and fixing the
names of R objects.
In this implementation, the empty string ""
has priority over
NA
which means NA
is only returned when all
values are NA
, e.g. str_coalesce(NA, NA)
.
str_coalesce(..., .args = NULL)
... |
Character vectors to coalesce. |
.args |
An alternative to |
str_coalesce(x, y)
is equivalent to
if_else(x != "" & !is.na(x), x, y)
.
A coalesced character vector of length corresponding to the recycled
size of supplied character vectors. See ?recycle
for details.
library(cheapr)
# Normal examples
str_coalesce("", "hello")
str_coalesce("", NA, "goodbye")
# '' always preferred
str_coalesce("", NA)
str_coalesce(NA, "")
# Unless there are only NAs
str_coalesce(NA, NA)
# `str_coalesce` is vectorised
x <- val_insert(letters, "", n = 10)
y <- val_insert(LETTERS, "", n = 10)
str_coalesce(x, y)
# Using `.args` instead of `do.call` is much more efficient
library(bench)
x <- cheapr_rep_len(list(letters), 10^3)
mark(do.call(str_coalesce, x),
str_coalesce(.args = x),
iterations = 50)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.