The better question is, how can I read it in R since the missing cells will already be NAs.
You may have used something like this:
read.csv(file, header=FALSE, strip.white = TRUE, sep=",")
Indicate lines NAlike this when you read this:
read.csv(file, header=FALSE, strip.white = TRUE, sep=",",
na.strings= c("999", "NA", " ", ""))
to answer your question. This approach may work:
dat <- data.frame(matrix(sample(c("", LETTERS[1:4]), 200,
replace=T, c(.6, rep(.1, 4))), 20))
blank2na <- function(x){
z <- gsub("\\s+", "", x)
x[z==""] <- NA
return(x)
}
data.frame(sapply(dat, blank2na))
source
share