[R] if function and apply

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Tue 01 Aug 2006 - 07:54:28 EST


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



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Aug 01 08:03:35 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 01 Aug 2006 - 22:17:35 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.