Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple na.strings in read.table() function in R

Tags:

r

na

read.table

I have a square table and it has two na.strings (e.g. "A" and "B") that I need to turn into NA. So far I can turn either one of those into NA but not both. How should I do this? Can I use a function in that argument? If yes, what function should I use? I tried like (na.strings = "A" | "B") and (na.strings = "A | B") but it does not work. My code is as follows:

loadfile<-read.table("test.csv", header=T, sep=",", na.strings="A | B")
like image 384
user2462924 Avatar asked Jun 07 '13 09:06

user2462924


1 Answers

na.strings takes a character vector, so...

loadfile <- read.table( "test.csv" , header = TRUE ,
                         sep="," ,
                         na.strings = c("A" , "B" ) )

From the helpfile:

na.strings: a character vector of strings which are to be interpreted as NA values

like image 135
Simon O'Hanlon Avatar answered Oct 19 '22 00:10

Simon O'Hanlon