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

luc*_*ano 5 excel r na read.csv

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

在此输入图像描述

Tyl*_*ker 18

更好的问题是如何将其读入R中,以便丢失的单元格已经是NAs.

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

read.csv(file, header=FALSE,  strip.white = TRUE, sep=",")
Run Code Online (Sandbox Code Playgroud)

NA在阅读时指定这样的字符串:

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

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

#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))
Run Code Online (Sandbox Code Playgroud)