[R] if function and apply

John Kane jrkrideau at yahoo.ca
Mon Jul 31 23:54:28 CEST 2006


Runninn R.2.3.1  Windows XP

I have a dataset just imported from SPSS.  It has any
number of 99's as missing data and it looks like the
next dataset will have custom missing codes. I have
abouat 120 variables and an N of 2000.

"I think" thatI would like to apply a function to the
data.frame (or to a matrix of the data if needed) to
recode all the 99's to NA.  I thought that I could
adapt an example from the list using "apply" but with
no success.  

Is there a decent source of examples of how to write
an if statement on the web? I'm missing something
simple.
Here is an example of what I have been trying.

######  
cat <- c( 3,5,6,8)
dog <- c(3,5,3,6)
rat <- c (5, NA, 4, 9)
mat <- (cbind(cat,dog, rat))
Df <- data.frame(cbind(cat, dog, rat)

#  define function
fn <- function (x a) {
if (x==a)return  (b) else x
}

apply(mat, c(1,2), fn, 99, NA)
#####################

Thanks



More information about the R-help mailing list