Re: [R] NA erase your data trick

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 17 May 2005 - 17:46:19 EST

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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue May 17 17:51:43 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:47 EST