使用XLConnect读取具有缺失值的.xls文件
当使用XLConnect
读取.xls文件时,如何设置缺失值。 类似于na.string==c()
中的na.string==c()
参数。
我目前的R代码如下所示:
install.packages("XLConnect") library(XLConnect) excel.file <- file.path(".../FakeData.xls") fake2 <- readWorksheetFromFile(excel.file, sheet="Sheet1", header=TRUE)
我的假数据是这样的:
ID Age Weight Gender 1 30 55 M 2 22 NA F 3 33 80 NA
我希望我的NA值被解释为缺失,所以权重和性别分别被读作R的数字/因子variables。 目前,由于NA值而被转换为字符variables。
当我尝试:
setMissingValue(fake2, value = c("NA"))
我得到以下错误:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'setMissingValue' for signature '"data.frame"'
您不得不在工作簿对象上调用setMissingValue
而不是文件名。
wb <- loadWorkbook(excel.file) setMissingValue(wb, value = c("")) fake2 <- readWorksheet(wb, sheet="Sheet1", header=TRUE)