Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created April 23, 2024 12:58
Show Gist options
  • Save PietrH/31953a485595cdf2ebb3fb846d438ffd to your computer and use it in GitHub Desktop.
Save PietrH/31953a485595cdf2ebb3fb846d438ffd to your computer and use it in GitHub Desktop.
paste0() but drop NA instead
str_replace_na <- function(string, replacement = ""){
string[is.na(string)] <- replacement
return(string)
}
c("a", NA, "c") |> str_replace_na(replacement = "") |> paste0(collapse = " ")
#> [1] "a c"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment