[R] Quick recode of -999 to NA in R

Marc Schwartz marc_schwartz at me.com
Wed Mar 30 19:34:15 CEST 2011


On Mar 30, 2011, at 11:52 AM, Gabor Grothendieck wrote:

> On Wed, Mar 30, 2011 at 11:51 AM, peter dalgaard <pdalgd at gmail.com> wrote:
>> 
>> On Mar 30, 2011, at 16:05 , Christopher Desjardins wrote:
>> 
>>>> 
>>>> dat0 <- read.table('tim1.dat', na = -999)
>>>> 
>>> 
>>> Ah ... yes. I knew that but clearly didn't at the time of my question or
>>> script writing.
>>> Thanks,
>>> Chris
>> 
>> Depending on where your data came from, you could get caught by the fact that the above is really ...na.strings="-999"... and that is not going to work if the actual code is (say) -999.00.
>> 
>> Another straightforward option is dat0[dat0 == -999] <- NA
> 
> That's a good point about -999.00.  If we knew all the variations in
> the input then a workaround for that would be to cover all of them:
> 
> read.table("myfile", ...whatever...,   na.strings = c("-999",
> "-999.0", "-999.00"))


There is also the seminal:

  is.na(dat0) <- dat0 == -999

See ?is.na, specifically the assignment variant.

Regards,

Marc Schwartz



More information about the R-help mailing list