Insert NA values into dataframe blank cells when importing read.csv/read.xlsx

luciano picture luciano · Aug 4, 2012 · Viewed 17.3k times · Source

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)?

enter image description here

Answer

Tyler Rinker picture Tyler Rinker · Aug 4, 2012

The better question is how can I read it into R so the missing cells will already be NAs.

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))