Skip to content
Prev 310828 / 398503 Next

Replacing string in matrix with zero

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:
______________________________________________
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.