Skip to content

NA erase your data trick

7 messages · Anders Schwartz Corr, Daniel Nordlund, Uwe Ligges +4 more

#
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?

B) How do I do it right next time, I learned my lesson, I'll never do it
again, I promise!

Anders Corr
+     for(n in 1:dim(tcn5)[1]){     ##for the number of rows
+         tcn5[is.na(tcn5[n,i]) | tcn5[n,i] == -9] <- NA
+
+         }
+ }
#
I don't think there is any going back.
How about something like

x[x == -9] <- NA

Dan Nordlund
Bothell, WA
#
Anders Schwartz Corr wrote:
As you got it the first time. There is nothing like "undo".
By vectorization:

    tcn5[tcn5 == -9] <- NA

Uwe Ligges
#
Anders Schwartz Corr wrote:
Not if you don't have it backed up somewhere else.

I wouldn't recommend keeping your only copy of anything in an R 
workspace.  It's too easy to accidentally delete or overwrite it.  Keep 
the original in a file.
For some values of i and n, this last line simplifies to

tcn5[TRUE] <- NA

which is why you lost your data.

You want to (a) think in vectors, or (b) use an if statement:

(a) Replace your whole series of statements with

tcn5[is.na(tcn5) | tcn5 == -9] <- NA

or

(b) Replace just the last line above with

   if (is.na(tcn5[n,i]) | tcn5[n,i] == -9) tcn5[n,i] <- NA

I'd choose (a); it's a lot cleaner and will run faster.

Duncan Murdoch
#
Hi

Maybe

tcn5[tcn5 == -9] <- NA

if tcn5 is matrix
Read some intro on data manipulation, it helps you to avoid 
thinking in loops

Cheers
Petr
On 17 May 2005 at 1:37, Anders Schwartz Corr wrote:

            
Petr Pikal
petr.pikal at precheza.cz
#
On Tue, 17 May 2005 08:33:00 +0200
Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:

            
If you??re lucky it still lives inside an old, not overwritten when leaving the depressing R session .RData.

Detlef
#
On Tue, 17 May 2005, Uwe Ligges wrote:

            
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.