The attached screenshot shows part of a dataframe which I have just imported into R from an excel file. In the cells which are blank, I need to insert 'NA'. How can I insert NA into any cell which is blank (whilst leaving the already populated cells alone)?
The better question is how can I read it into R so the missing cells will already be NA
s.
Maybe you used something like this:
read.csv(file, header=FALSE, strip.white = TRUE, sep=",")
Specify the NA
strings like this when you read it in:
read.csv(file, header=FALSE, strip.white = TRUE, sep=",",
na.strings= c("999", "NA", " ", ""))
to actually answer your question. This approach could work:
#making fake data on a Saturday morning
dat <- data.frame(matrix(sample(c("", LETTERS[1:4]), 200,
replace=T, c(.6, rep(.1, 4))), 20))
#function to replace blanks with missing
blank2na <- function(x){
z <- gsub("\\s+", "", x) #make sure it's "" and not " " etc
x[z==""] <- NA
return(x)
}
#apply that function
data.frame(sapply(dat, blank2na))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With