Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to replace NA values using stringr is to use the strreplaceall() function. Here is an example code:

library(stringr)

# create a character vector with NA values
my_vec <- c("hello", NA, "world", NA, "!")
print(my_vec)

# replace NA values with "N/A"
my_vec_new <- str_replace_all(my_vec, "NA", "N/A")
print(my_vec_new)

Output:

[1] "hello" NA      "world" NA      "!"
[1] "hello" "N/A"   "world" "N/A"   "!"

In this example, we use the strreplaceall() function to replace all occurrences of "NA" with "N/A" in the character vector, including the NA values.