Reputation: 291
I'm trying to read an xlsx file where a whole column contains "NA", but I would like R to interpret it as NA instead of the character "NA". I use this line of code:
assignments <- read.xlsx(file="assignments.xlsx", sheetIndex=2, header=T, stringsAsFactors=FALSE)
Thanks in advance
Upvotes: 0
Views: 3384
Reputation: 1
I have no experience with read.xlsx
function, which on the basis of documentation appears to not accept the na.strings
option that is allowed in read.table
for example. Thus what I would do is export the xlsx file as .csv
, and then use the na.strings
option, as in
assignments <- read.table(file="assignments.csv", header=TRUE, stringsAsFactors=FALSE, na.strings = "NA",sep=",")
where the sep
option should be whatever the separator in the exported file happens to be.
Another option of course is to just convert "NA"
string into an NA
value afterwards, as suggested by Adrian in his answer.
Upvotes: 0
Reputation: 7457
The following works for me
data <- read.xlsx(file = "test.xlsx", header = TRUE)
data[data == "NA"] <- NA
Upvotes: 1