Ask Your Question
2

How can NA values be replaced using stringr?

asked 2023-07-08 16:15:14 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-08 16:18:02 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-08 16:15:14 +0000

Seen: 10 times

Last updated: Jul 08 '23