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(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 + + } + }>
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] > On Behalf Of Anders Schwartz Corr > Sent: Monday, May 16, 2005 10:38 PM > To: r-help at stat.math.ethz.ch > Subject: [R] NA erase your data trick > > > 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?I don't think there is any going back.> > B) How do I do it right next time, I learned my lesson, I'll never do it > again, I promise! >How about something like x[x == -9] <- NA Dan Nordlund Bothell, WA> Anders Corr > > > 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 > + > + } > + } > > > > ______________________________________________ > R-help at 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
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 Uwe Ligges> Anders Corr > > >>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 > + > + } > + } > > > ______________________________________________ > R-help at 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
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?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.> > B) How do I do it right next time, I learned my lesson, I'll never do it > again, I promise! > > Anders Corr > > >>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] <- NAFor 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> mat<-matrix(rnorm(100),10,10) > mat[5,6:7]<- -9 > mat[mat == -9]<-NARead 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:> > 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(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 > + > + } > + } > > > > ______________________________________________ > R-help at 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.htmlPetr Pikal petr.pikal at precheza.cz