5

I'm trying to read into R an excel sheet that uses multiple values for NA (specifically, "N/A" and "n/a"). If I try to give na= a list of strings it throws an error:

read_excel(path = "file.xlsx",
           na = "N/A") #This works just fine

read_excel(path = "file.xlsx",
           na = c("N/A", "n/a"))

Error in eval(substitute(expr), envir, enclos) : expecting a single value

Any ideas on how to read this in with both strings converted to NA? Or am I better off doing a find/replace once the data is in R?

Watanake
  • 263
  • 3
  • 9
  • 1
    According to [`readxl/issues/272`](https://github.com/tidyverse/readxl/issues/272), "multiple-`na` values" is supported in the github version. (I have not yet tested it.) – r2evans Mar 06 '17 at 21:07
  • Thanks r2evans, the github version is working great now – Watanake Mar 06 '17 at 21:44
  • readxl::read_excel from readxl version 1.1.0 offers this feature now. – tjebo May 21 '19 at 17:40

1 Answers1

4

As you gathered, read_excel does not accept more than one value. Consider using gdata::read.xls instead.

gdata::read.xls("file.xlsx", na.strings = c("N/A", "n/a"))

Edit: Note that you need to have perl installed to run this. If you're on windows you may need to specify something like perl="C:/Perl/bin/perl.exe" in the call to read.xls.

Edit 2: As @r2evans suggested in the comments, the development version of readxl supports multiple na values:

devtools::install_github("tidyverse/readxl")
readxl::read_excel(path = "file.xlsx", na = c("N/A", "n/a"))
Johan Larsson
  • 3,496
  • 18
  • 34
  • Thanks for the continued help, I just spent awhile installing perl, setting the path and running installXLSXsupport(), still giving me an empty data frame. BUT, if this github version works that will be all I wanted and more, downloading now. Thanks! – Watanake Mar 06 '17 at 21:30
  • This worked perfectly. Took a bit to install the package (didn't have Rtools), and then I had to manually delete Rccp because the old version was blocking installation (this answer helped: http://stackoverflow.com/questions/26570912/error-in-installation-a-r-package). But now read_excel is doing everything I need, thanks again! – Watanake Mar 06 '17 at 21:44