Dear all, Suppose I have a data frame like this: [code] var1 <- c(1,999,2) var2 <- c(999,1,2) var3 <- c(1,2,999) example <- data.frame(var1,var2,var3) [/code] I want to replace all 999 to NA in all observations in all columns. I know how to do it in each individual column. [code] example$var1[example$var1==999] <- NA [/code] I think it can be done with a for loop. [code] for (i in 1:length(example)) { example[,i][example[,i] == 999] <- NA } [/code] Is there any R-esque way to do this without using a for loop? Thanks. Regards, CH -- CH Chan
Jorge I Velez
2011-Aug-31 08:54 UTC
[R] Recoding observations in all columns of a data frame.
Dear CH, Try example[example == 999] <- NA example HTH, Jorge On Wed, Aug 31, 2011 at 4:48 AM, C.H. <> wrote:> Dear all, > > Suppose I have a data frame like this: > > [code] > var1 <- c(1,999,2) > var2 <- c(999,1,2) > var3 <- c(1,2,999) > example <- data.frame(var1,var2,var3) > [/code] > > I want to replace all 999 to NA in all observations in all columns. I > know how to do it in each individual column. > > [code] > example$var1[example$var1==999] <- NA > [/code] > > I think it can be done with a for loop. > > [code] > for (i in 1:length(example)) > { > example[,i][example[,i] == 999] <- NA > } > [/code] > > Is there any R-esque way to do this without using a for loop? > > Thanks. > > Regards, > > CH > -- > CH Chan > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]