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

允我心安 提交于 2019-12-03 08:21:23

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))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!