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
Pankaj Kaundal
  • 1,012
  • 3
  • 13
  • 25
  • It's better to leave it as `NA`s. Especially if that column is a `factor`. Other than that, a simple Google search gives all you need. Have you saw this http://stackoverflow.com/questions/8161836/how-do-i-replace-na-values-with-zeros-in-r ? – David Arenburg Feb 02 '16 at 07:59
  • 2
    Use this: `df$B[is.na(df$B)] <- "Not Found"` – Tim Biegeleisen Feb 02 '16 at 08:01

1 Answers1

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
pe-perry
  • 2,591
  • 2
  • 22
  • 33