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

空扰寡人 提交于 2019-11-29 04:52:19

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

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!

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