With the R package xlsx, is it possible to set na.strings when reading an Excel file?

只谈情不闲聊 提交于 2019-11-27 17:01:22

问题


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

Is there some other way to do it?


回答1:


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

Or, transform your data to a csv and use read.csv , or as commented use another package that take care of missing values.

Edit use XLConnect package:

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



回答2:


I don't think it is possible to do but you can easily use apply to do that after you have loaded the excel file:

Data <- data.frame(apply(Data,1:2,function(x) if( x %in% 'no info') return(NA) else return(x)))

Obviously where 'no info' you can have your own na.strings vector

Hope that helps!



来源:https://stackoverflow.com/questions/26743036/with-the-r-package-xlsx-is-it-possible-to-set-na-strings-when-reading-an-excel

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