0

I have a df where a bunch of missing data came in as empty character strings, so "" instead of NA or something useful.

I have tried to replace it with NA using this code:

df[df == 0] <- NA

Which in mine is:

nc_allparcels[nc_allparcels == ""] <- NA

However, the error I get is:

Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

Any suggestions for fixing this? Thanks!

tchoup
  • 971
  • 4
  • 11

1 Answers1

0

You can try the function:

naniar::replace_with_na_all

A <- c("a", "b", "c", "")
B <- c(",", ".", "", "-")
df <- data.frame(A, B)

naniar::replace_with_na_all(data = df, condition = ~.x == "")

OR if just certain columns should be modified

naniar::replace_with_na_at(data = df, .vars = "A", condition = ~.x == "")
naniar::replace_with_na_at(data = df, .vars = c("A", "B"), condition = ~.x == "")
Markus Bauer
  • 144
  • 1
  • 9
  • Hi, I gave this a go but it took too long to do the whole file (left it running overnight and was still going this morning). I tried to speciffy it to the specific column that I cared about as: replace_with_na_all(data = df$column, condition = ~.x == ""), but that reduced all observations to 1. Am I doing this wrong? – tchoup May 26 '21 at 15:37