AW: [R] Replacing all NA values in a matrix

TEMPL Matthias Matthias.Templ at statistik.gv.at
Wed Jan 5 15:55:27 CET 2005


Replacing the NA´s with eg. 1 :

> a
   1   2  3   4
1 20  50 10  80
2 NA  19 NA  49
3 NA  32 NA  61
4 45 101 44 190


a[try(is.na(a)) == TRUE] <- 1

> a
   1   2  3   4
1 20  50 10  80
2  1  19  1  49
3  1  32  1  61
4 45 101 44 190

I this can help you,
Matthias


> -----Ursprüngliche Nachricht-----
> Von: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von 
> michael watson (IAH-C)
> Gesendet: Mittwoch, 05. Jänner 2005 15:26
> An: R-help at stat.math.ethz.ch
> Betreff: [R] Replacing all NA values in a matrix
> 
> 
> OK, dumb question, and it is probably in the docs somewhere, 
> but after 12 months working with R and quite a while looking 
> at the docs, I still don't know (or have forgotten) how to 
> replace all NA values in a matrix at once with some other 
> value.  I can do it column by column using is.na(), but I 
> can't figure out how to do it for the whole matrix.  My 
> apologies, I am ashamed ;-)
> 
> Michael Watson
> Head of Informatics
> Institute for Animal Health,
> Compton Laboratory,
> Compton,
> Newbury,
> Berkshire RG20 7NN
> UK
> 
> Phone : +44 (0)1635 578411 ext. 2535
> Mobile: +44 (0)7990 827831
> E-mail: michael.watson at bbsrc.ac.uk
> 
> ______________________________________________
> R-help at 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
>




More information about the R-help mailing list