I agree that the problem is best solved at read-in, by setting na.strings = c("", "N/A", "n/a")
in read.table
, as suggested by @Darren Tsai. If that's no longer an option because you've processed the data already and, as I suspect, you do not want to keep only complete cases, as suggested by @Rui Barradas, then the issue can be addressed this way:
DATA:
df_final <- data.frame(v1 = c(1, "N/A", 2, "n/a", "", 3),
v2 = c("a", "", "b", "c", "d", "N/A"))
df_final
v1 v2
1 1 a
2 N/A
3 2 b
4 n/a c
5 d
6 3 N/A
SOLUTION:
To introduce NA
into empty fields, you can do:
df_final[df_final==""] <- NA
df_final
v1 v2
1 1 a
2 N/A <NA>
3 2 b
4 n/a c
5 <NA> d
6 3 N/A
To change the other values into NA
, you can use lapply
and a function:
df_final[,1:2] <- lapply(df_final[,1:2], function(x) gsub("N/A|n/a", NA, x))
df_final
v1 v2
1 1 a
2 <NA> <NA>
3 2 b
4 <NA> c
5 <NA> d
6 3 <NA>