I am trying to replace a set of typos in a df
,
This is what I've got so far:
master_df <- invisible(
data.frame(lapply(master_df,
function(x) replace(x, as.matrix(x) == c("?", '-',''), NA))))
However the output looks as follows:
# a b c
#1 <NA>
#2 ? <NA> <NA>
#3 1 2 1
#4 2 3 2
#5 3 4 3
And throws the next warnings:
Warning messages: 1: In as.matrix(x) == c("?", "-", "") : longitud de objeto mayor no es múltiplo de la longitud de uno menor
2: In as.matrix(x) == c("?", "-", "") : longitud de objeto mayor no es múltiplo de la longitud de uno menor
3: In as.matrix(x) == c("?", "-", "") : longitud de objeto mayor no es múltiplo de la longitud de uno menor
The idea is that the set of typos c('?', '-', '')
are replaced by NA
in the whole df.
How could I accomplish this task?
data
master_df <- structure(list(a = c("", "?", "1", "2", "3"), b = c("", NA, "2",
"3", "4"), c = c(NA, NA, "1", "2", "3")), class = "data.frame", row.names = c(NA,
-5L))