Hello, I have some Inf values in a matrix, but I don't want to replace them with some value but rather remove the rows that contain the Inf values. Also I would like to record the rows which were removed. Is there an easy way to do this instead of writing loops over the matrix? Thanks. Ita Cirovic Donev
try the following: mat <- matrix(rnorm(40), 10, 4) mat[sample(20, 5)] <- Inf mat ########## index <- rowSums(!is.finite(mat)) >= 1 mat. <- mat[!index, ] which(index) 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/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <Ita.Cirovic-Donev at hypo-alpe-adria.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, February 14, 2006 5:01 PM Subject: [R] Inf values in a matrix> > > > > Hello, > > I have some Inf values in a matrix, but I don't want to replace them > with > some value but rather remove the rows that contain the Inf values. > Also I > would like to record the rows which were removed. Is there an easy > way to > do this instead of writing loops over the matrix? Thanks. > > Ita Cirovic Donev > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi see ?is.infinite ?which something like y <- x[-which(is.infinite(x), arr.ind=T)[,1],] y.removed <-x[which(is.infinite(x), arr.ind=T)[,1],] shall make the trick. On 14 Feb 2006 at 17:01, Ita.Cirovic-Donev at hypo-alpe-a wrote: To: r-help at stat.math.ethz.ch From: Ita.Cirovic-Donev at hypo-alpe-adria.com Date sent: Tue, 14 Feb 2006 17:01:12 +0100 Subject: [R] Inf values in a matrix> > > > > Hello, > > I have some Inf values in a matrix, but I don't want to replace them > with some value but rather remove the rows that contain the Inf > values. Also I would like to record the rows which were removed. Is > there an easy way to do this instead of writing loops over the matrix? > Thanks. > > Ita Cirovic Donev > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
Ita.Cirovic-Donev at hypo-alpe-adria.com wrote:> > > > Hello, > > I have some Inf values in a matrix, but I don't want to replace them with > some value but rather remove the rows that contain the Inf values. Also I > would like to record the rows which were removed. Is there an easy way to > do this instead of writing loops over the matrix? Thanks. > > Ita Cirovic Donev > > ______________________________________________ > 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.htmlExample: A <- matrix(1:100, 10) A[cbind(3:4, 5:6)] <- Inf recorded <- which(apply(A, 1, function(x) any(is.infinite(x)))) A[-recorded,] Uwe Ligges