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
Doh, replace() does the job just fine. Sheesh, I'm not coping well with work post christmas ;-) -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of michael watson (IAH-C) Sent: 05 January 2005 14:26 To: R-help at stat.math.ethz.ch Subject: [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
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
x <- matrix(rnorm(100),ncol=10) x[x>0] <- NA x[is.na(x)] <- 1000> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > michael watson (IAH-C) > Sent: Wednesday, January 05, 2005 9:26 AM > To: R-help at stat.math.ethz.ch > Subject: [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 >
What you should keep in mind is that a matrix in R is nothing more than a vector (formed by stacking the columns of the matrix) with the dim attribute. Thus you can do what you want to do by treating the matrix as a vector; e.g., mymat[is.na(mymat)] <- myFavoriteValue HTH, Andy> From: michael watson (IAH-C) > > 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 > >
Hi Michael,
try this:
mat <- matrix(1:25, 5, 5)
mat[sample(25, 10)] <- NA
mat
#####
mat[is.na(mat)] <- 100
mat
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk>
To: <R-help at stat.math.ethz.ch>
Sent: Wednesday, January 05, 2005 3:26 PM
Subject: [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
>
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 ;-)Since you can index a matrix like a vector (it's a vector with a dim attribute), you can simply say something like: X[is.na(X)] <- 99999 Uwe Ligges> 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
Dear Michael, A[is.na(A)] <- value I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > michael watson (IAH-C) > Sent: Wednesday, January 05, 2005 9:26 AM > To: R-help at stat.math.ethz.ch > Subject: [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
Michael -- is.na works on the full matrix. The commands below construct a matrix, insert some NA's, and then convert them all to 0.> temp1 <- matrix(runif(25), 5, 5) > temp1[temp1 < 0.1] <- NA > temp1[is.na(temp1)] <- 0Hope this helps. Regards, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of michael watson (IAH-C) Sent: Wednesday, January 05, 2005 9:26 AM To: R-help at stat.math.ethz.ch Subject: [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