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

旧街凉风 提交于 2020-01-01 03:23:13

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/11809854/insert-na-values-into-dataframe-blank-cells-when-importing-read-csv-read-xlsx

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