You test matrix did not have any 99 in it, so I replaced all '3's with
NAs
> mat
cat dog rat
[1,] 3 3 5
[2,] 5 5 NA
[3,] 6 3 4
[4,] 8 6 9> mat[mat == 3] <- NA
> mat
cat dog rat
[1,] NA NA 5
[2,] 5 5 NA
[3,] 6 NA 4
[4,] 8 6 9>
You can not do "value == NA"; you have to use 'is.na(value)'
On 7/31/06, John Kane <jrkrideau@yahoo.ca> wrote:>
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
[[alternative HTML version deleted]]