[R] ignoring zeros or converting to NA
Daniel Malter
daniel at umd.edu
Thu Aug 14 22:46:19 CEST 2008
Sorry for jumping in. I haven't read the entire conversation. But imagine you
want to compute 1/x for the entire matrix and your matrix has 0s and NAs.
Then you could do:
##define function
f<-function(x){1/x}
##sample data
y=c(0,1,2,3,4,5,6,7,NA)
##arrange in matrix
mat=matrix(y,3,3)
##apply function to matrix "mat"
##except 0s and NAs
calc.mat=ifelse(mat==0|is.na(mat)==T,NA,f(mat))
##inspect resulting matrix
calc.mat
If you are concerned about near zeros instead of zeros, you could adjust the
ifelse condition.
Best,
Daniel
rcoder wrote:
>
> Hi everyone,
>
> I have a matrix that has a combination of zeros and NAs. When I perform
> certain calculations on the matrix, the zeros generate "Inf" values. Is
> there a way to either convert the zeros in the matrix to NAs, or only
> perform the calculations if not zero (i.e. like using something similar to
> an !all(is.na() construct)?
>
> Thanks,
>
> rcoder
>
--
View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18989410.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list