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)
Run the code above in your browser using DataLab