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?
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With