使用R包xlsx,读取Excel文件时是否可以设置na.strings? [英] With the R package xlsx, is it possible to set na.strings when reading an Excel file?
问题描述
我正在使用 read.xlsx
在Excel文件中阅读,我想使用读取来设置na.strings。表
。这可能吗?只需将na.strings添加到这样的调用中就不行:
I'm reading in an Excel file using read.xlsx
, and I would like to set na.strings as you can with read.table
. Is this possible? It doesn't work to just add na.strings to the call like this:
Data <- read.xlsx("my file.xlsx", sheetName = "MyData", na.strings = "no info")
还有其他一些方法吗?
推荐答案
没有这个不可能的简单的原因, read.xlsx
不会处理特殊的缺失值。但这可能是对 getCellvalue
函数的一个可能的增强。
No this is not possible for the simple reason that read.xlsx
doesn't take care of special missing values. But this can be a possible enhancement for getCellvalue
function.
您可以使用以下方式替换缺少的值:
You can either replace missing values using something like :
Data[Data=="no info"] <- NA
或者,将您的数据转换为csv并使用 read.csv
,或者作为注释使用另一个处理缺少值的包。
Or, transform your data to a csv and use read.csv
, or as commented use another package that take care of missing values.
性能越高 XLConnect
package使用 setMissingValue
函数来处理缺少的值。这里的等效代码可以写成:
The more performant XLConnect
package takes care of missing values using setMissingValue
function. Here the equivalent code can be written as:
library("XLConnect")
wb <- loadWorkbook("my file.xlsx")
setMissingValue(wb, value = "no info")
readWorksheet(wb, sheet = "MyData")
这篇关于使用R包xlsx,读取Excel文件时是否可以设置na.strings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!