Dear All, I have a matrix in which the diagonal has the string "Inf" in it. In order to be able to do cluster analysis this needs to be replaced with a Zero. I can do this by putting it into Excel, replacing and putting it back into R but it's tedious, and I am sure there is a simple way to do it in R. If you have the route to do this, it would be much appreciated. Best wishes Nick Duncan
Replacing string in matrix with zero
6 messages · Jeremy Miles, Rui Barradas, arun +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121114/aa83eef3/attachment.pl>
Hello, I doubt your Inf is a string, R does have the numeric value Inf (and -Inf), just try 1/0. As for the question, x <- matrix(1:9, ncol = 3) diag(x) <- 1/0 x x[x == Inf] <- 0 x Note that it could also be diag(x) <- 0 Hope this helps, Rui Barradas Em 14-11-2012 22:13, Nick Duncan escreveu:
Dear All, I have a matrix in which the diagonal has the string "Inf" in it. In order to be able to do cluster analysis this needs to be replaced with a Zero. I can do this by putting it into Excel, replacing and putting it back into R but it's tedious, and I am sure there is a simple way to do it in R. If you have the route to do this, it would be much appreciated. Best wishes Nick Duncan
______________________________________________ R-help at r-project.org 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.
HI, If it is value Inf, you could also use ?is.infinite() ? x<-matrix(c(Inf,1,2,3,Inf,5,6,7,Inf),nrow=3) x[is.infinite(x)]<-0 ?x #???? [,1] [,2] [,3] #[1,]??? 0??? 3??? 6 #[2,]??? 1??? 0??? 7 #[3,]??? 2??? 5??? 0 A.K. ----- Original Message ----- From: Rui Barradas <ruipbarradas at sapo.pt> To: Nick Duncan <nickdunc at gmail.com> Cc: r-help at r-project.org Sent: Wednesday, November 14, 2012 7:14 PM Subject: Re: [R] Replacing string in matrix with zero Hello, I doubt your Inf is a string, R does have the numeric value Inf (and -Inf), just try 1/0. As for the question, x <- matrix(1:9, ncol = 3) diag(x) <- 1/0 x x[x == Inf] <- 0 x Note that it could also be diag(x) <- 0 Hope this helps, Rui Barradas Em 14-11-2012 22:13, Nick Duncan escreveu:
Dear All, I have a matrix in which the diagonal has the string "Inf" in it. In order to be able to do cluster analysis this needs to be replaced with a Zero. I can do this by putting it into Excel, replacing? and putting it back into R but it's tedious, and I am sure there is a simple way to do it in R. If you have the route to do this, it would be much appreciated. Best wishes Nick Duncan
______________________________________________ R-help at r-project.org 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.
______________________________________________ R-help at r-project.org 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.
On 15/11/12 11:13, Nick Duncan wrote:
Dear All, I have a matrix in which the diagonal has the string "Inf" in it. In order to be able to do cluster analysis this needs to be replaced with a Zero.
I am very sceptical about this assertion. "Inf" means infinity
which is of course *NOT* the same as 0. By anyone's standards.
I know nothing about cluster analysis, so I may be toadally out
to lunch in this, but I suspect that you need to re-think.
cheers,
Rolf Turner
<SNIP>
Dear Rolf, You are of course quite right, however I wish the diagonal to be ignored by setting the value to zero. The inf was just recognising the flow value from self to self. A very fair point though. Best, Nick
On 15 November 2012 04:10, Rolf Turner <rolf.turner at xtra.co.nz> wrote:
On 15/11/12 11:13, Nick Duncan wrote:
Dear All, I have a matrix in which the diagonal has the string "Inf" in it. In order to be able to do cluster analysis this needs to be replaced with a Zero.
I am very sceptical about this assertion. "Inf" means infinity
which is of course *NOT* the same as 0. By anyone's standards.
I know nothing about cluster analysis, so I may be toadally out
to lunch in this, but I suspect that you need to re-think.
cheers,
Rolf Turner
<SNIP>
An Englishman, even if he is alone, forms an orderly queue of one. --George Mikes