[R] Replacing all NA values in a matrix
Sean Davis
sdavis2 at mail.nih.gov
Wed Jan 5 15:44:43 CET 2005
You have it already....
> h <- matrix(rnorm(100),nrow=20)
> h
[,1] [,2] [,3] [,4] [,5]
[1,] 0.4669801 0.3349176 -1.60686041 0.981491440 0.1627222
[2,] -0.2580262 -0.2620413 0.53852801 1.294129626 -0.1632906
[3,] 0.9654591 1.0077212 -0.45603772 1.845272884 0.2910091
[4,] 1.0710281 1.6234312 0.19610087 0.524864299 -0.1197756
[5,] 0.3727526 -1.0742054 -0.46485338 -0.128577874 0.5177477
[6,] 0.7289514 1.9020074 0.34534408 -0.313550835 0.7026291
[7,] -0.3037257 0.3922162 -1.77990093 0.596858216 -0.4039951
[8,] -1.7857808 0.4271333 -1.32907071 -0.596656935 0.4008593
[9,] 1.4643707 -0.4369587 0.93522859 -0.948929936 0.1416290
[10,] 0.1243959 -1.4509269 -0.39656577 -0.550951866 1.2189326
[11,] 0.7210016 -0.2337671 0.70075393 -1.034782089 -1.7652139
[12,] 0.5509319 -0.9731717 0.12392721 0.421338123 -1.3197952
[13,] 1.8718778 -0.2853116 0.69003178 0.939630649 0.7421644
[14,] -0.3897884 -1.4627226 -0.32424877 1.115026790 -0.3912558
[15,] -1.5784201 0.6987771 -0.29907714 0.816135639 -0.8182227
[16,] -0.6140077 -0.1025945 0.04281918 0.006010866 1.0701661
[17,] -1.0290960 0.1830102 -0.51057604 -1.034981163 -1.5717075
[18,] -0.0902371 1.6820050 -0.40896428 -1.560638110 1.1076107
[19,] -1.6744785 2.2675648 0.27397118 1.223144752 -0.9754583
[20,] -0.2265682 0.1512010 0.60412290 0.585478462 0.5247539
> h[h<0] <- NA
> h
[,1] [,2] [,3] [,4] [,5]
[1,] 0.4669801 0.3349176 NA 0.981491440 0.1627222
[2,] NA NA 0.53852801 1.294129626 NA
[3,] 0.9654591 1.0077212 NA 1.845272884 0.2910091
[4,] 1.0710281 1.6234312 0.19610087 0.524864299 NA
[5,] 0.3727526 NA NA NA 0.5177477
[6,] 0.7289514 1.9020074 0.34534408 NA 0.7026291
[7,] NA 0.3922162 NA 0.596858216 NA
[8,] NA 0.4271333 NA NA 0.4008593
[9,] 1.4643707 NA 0.93522859 NA 0.1416290
[10,] 0.1243959 NA NA NA 1.2189326
[11,] 0.7210016 NA 0.70075393 NA NA
[12,] 0.5509319 NA 0.12392721 0.421338123 NA
[13,] 1.8718778 NA 0.69003178 0.939630649 0.7421644
[14,] NA NA NA 1.115026790 NA
[15,] NA 0.6987771 NA 0.816135639 NA
[16,] NA NA 0.04281918 0.006010866 1.0701661
[17,] NA 0.1830102 NA NA NA
[18,] NA 1.6820050 NA NA 1.1076107
[19,] NA 2.2675648 0.27397118 1.223144752 NA
[20,] NA 0.1512010 0.60412290 0.585478462 0.5247539
> h[is.na(h)] <- 0
> h
[,1] [,2] [,3] [,4] [,5]
[1,] 0.4669801 0.3349176 0.00000000 0.981491440 0.1627222
[2,] 0.0000000 0.0000000 0.53852801 1.294129626 0.0000000
[3,] 0.9654591 1.0077212 0.00000000 1.845272884 0.2910091
[4,] 1.0710281 1.6234312 0.19610087 0.524864299 0.0000000
[5,] 0.3727526 0.0000000 0.00000000 0.000000000 0.5177477
[6,] 0.7289514 1.9020074 0.34534408 0.000000000 0.7026291
[7,] 0.0000000 0.3922162 0.00000000 0.596858216 0.0000000
[8,] 0.0000000 0.4271333 0.00000000 0.000000000 0.4008593
[9,] 1.4643707 0.0000000 0.93522859 0.000000000 0.1416290
[10,] 0.1243959 0.0000000 0.00000000 0.000000000 1.2189326
[11,] 0.7210016 0.0000000 0.70075393 0.000000000 0.0000000
[12,] 0.5509319 0.0000000 0.12392721 0.421338123 0.0000000
[13,] 1.8718778 0.0000000 0.69003178 0.939630649 0.7421644
[14,] 0.0000000 0.0000000 0.00000000 1.115026790 0.0000000
[15,] 0.0000000 0.6987771 0.00000000 0.816135639 0.0000000
[16,] 0.0000000 0.0000000 0.04281918 0.006010866 1.0701661
[17,] 0.0000000 0.1830102 0.00000000 0.000000000 0.0000000
[18,] 0.0000000 1.6820050 0.00000000 0.000000000 1.1076107
[19,] 0.0000000 2.2675648 0.27397118 1.223144752 0.0000000
[20,] 0.0000000 0.1512010 0.60412290 0.585478462 0.5247539
>
Sean
On Jan 5, 2005, at 9:26 AM, michael watson ((IAH-C)) wrote:
> 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