# [R] NA erase your data trick

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 17 09:46:19 CEST 2005

```On Tue, 17 May 2005, Uwe Ligges wrote:

> Anders Schwartz Corr wrote:
>> Oops,
>>
>> I just erased all my data using this gizmo that I thought would replace -9
>> with NA.
>>
>> A) Can I get my tcn5 back?
>
> As you got it the first time. There is nothing like "undo".
>
>
>> B) How do I do it right next time, I learned my lesson, I'll never do it
>> again, I promise!
>
> By vectorization:
>
>   tcn5[tcn5 == -9] <- NA

That will work if tcn5 contains NAs, but only because NA indices on the
lhs are now ignored for matrices (if tcn5 is a matrix, which seems
unstated) -- this used not to be the case.  I would prefer

tcn5[tcn %in% -9] <- NA

Using %in% rather than == in computed indices is a good habit to acquire:
it also makes things like

tcn5[tcn %in% c(-9, -99)] <- NA

work as expected.

If tcn is a data frame, you have to do this column-by-column, as in

tcn5[] <- lapply(tcn5, function(x) x[x %in% -9] <- NA)

or by a logical index matrix, which is harder to construct.

>>> for(i in 1:dim(tcn5)[2]){         ##for the number of columns
>>
>> +     for(n in 1:dim(tcn5)[1]){     ##for the number of rows
>> +         tcn5[is.na(tcn5[n,i]) | tcn5[n,i] == -9] <- NA
>> +
>> +         }
>> + }

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```