导入read.csv / read.xlsx时,将NA值插入数据框空白单元格

问题描述 投票:5回答:1

附带的屏幕截图显示了我刚从excel文件导入R的数据帧的一部分。在空白的单元格中,我需要插入'NA'。如何将NA插入任何空白的细胞中(同时留下已经填充的细胞)?

r excel na read.csv
1个回答
18
投票

更好的问题是如何将其读入R中,以便丢失的细胞已经是NAs。

也许你使用过这样的东西:

read.csv(file, header=FALSE,  strip.white = TRUE, sep=",")

在阅读时,请指定NA字符串,如下所示:

read.csv(file, header=FALSE,  strip.white = TRUE, sep=",",
    na.strings= c("999", "NA", " ", ""))  

实际回答你的问题。这种方法可行:

#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))
© www.soinside.com 2019 - 2024. All rights reserved.