Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

excel

na

xlsx

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?

like image 604
shirewoman2 Avatar asked Nov 04 '14 19:11

shirewoman2


2 Answers

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")
like image 121
agstudy Avatar answered Oct 19 '22 11:10

agstudy


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!

like image 33
LyzandeR Avatar answered Oct 19 '22 12:10

LyzandeR