[R] Replace NAs in dataframe: what am I doing wrong

Sébastien pomchip at free.fr
Sun Aug 12 00:20:33 CEST 2007


Dear R-users,

My script imports a dataset from a csv file, in which missing values are 
represented by ".". This importation is done into a dataframe using the 
read.table function with na.strings = "."  Then I want to replace the 
NAs in the first column of the dataframe by "Missing data". I am using 
the following code to do so :

mydata<-data.frame(read.table(myFile,sep=",",header=TRUE,na.strings="."))   
   # myFile is the full path of the source file

mydata[,1][is.na(mydata[,1])]<-"Missing value"

This code works perfectly fine if this first column contains only 
missing values, i.e. ".". As soon as it contains multiple levels and 
missing values, things start to get wrong. I get the following error 
message and the replacement is not done.

Warning message:
invalid factor level, NAs generated in: `[<-.factor`(`*tmp*`, 
is.na(mydata[, 1]), value = "Missing value")

Is there an error in my code or is that a bug (I doubt about it) ?

Thanks in advance.



More information about the R-help mailing list