1

I want all NAs to be replaced with "Not Found" in a df .

i have this df

A    B
1    NA
2    NA
3    NA

How can i get that .

A    B
1    Not Found
2    Not Found
3    Not Found
2
  • It's better to leave it as NAs. Especially if that column is a factor. Other than that, a simple Google search gives all you need. Have you saw this stackoverflow.com/questions/8161836/… ? Commented Feb 2, 2016 at 7:59
  • 2
    Use this: df$B[is.na(df$B)] <- "Not Found" Commented Feb 2, 2016 at 8:01

1 Answer 1

3

You can Assign "Not Found" to df[is.na(df)]. However, it will cause errors if some columns are factors.

df <- data.frame(A = 1:3, B = rep(NA, 3), stringsAsFactors = FALSE)
df
#   A  B
# 1 1 NA
# 2 2 NA
# 3 3 NA
 df[is.na(df)] <- "Not Found"
df
#   A         B
# 1 1 Not Found
# 2 2 Not Found
# 3 3 Not Found
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.