[R] Recoding observations in all columns of a data frame.

C.H. chainsawtiney at gmail.com
Wed Aug 31 10:48:55 CEST 2011


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



More information about the R-help mailing list