i want to replace certain values of a matrix. i know how to replace them when the values are in a vector. i tried the same way for a matrix and got this answer: la[la==0]<-1 Error in [<-.data.frame(*tmp*, la == 0, value = 1) : matrix subscripts not allowed in replacement i found a way to do this with "for()", but i think this is the worst way to do it. So is there a better way? Thomas J. Pesl -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 29-Aug-00 Pesl Thomas wrote:> i want to replace certain values of a matrix. > i know how to replace them when the values are in a vector. > i tried the same way for a matrix and got this answer: > > la[la==0]<-1 > Error in [<-.data.frame(*tmp*, la == 0, value = 1) : > matrix subscripts not allowed in replacement > > i found a way to do this with "for()", > but i think this is the worst way to do it. > > So is there a better way?Try this: la <- ifelse(la == 0, 1, la) Martyn -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> i want to replace certain values of a matrix. > i know how to replace them when the values are in a vector. > i tried the same way for a matrix and got this answer: > > la[la==0]<-1 > Error in [<-.data.frame(*tmp*, la == 0, value = 1) : > matrix subscripts not allowed in replacementla is a data.frame, not a matrix: R> x <- diag(10) R> x[x == 0] <- 1 R> x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 1 1 1 1 1 1 1 1 [2,] 1 1 1 1 1 1 1 1 1 1 [3,] 1 1 1 1 1 1 1 1 1 1 [4,] 1 1 1 1 1 1 1 1 1 1 [5,] 1 1 1 1 1 1 1 1 1 1 [6,] 1 1 1 1 1 1 1 1 1 1 [7,] 1 1 1 1 1 1 1 1 1 1 [8,] 1 1 1 1 1 1 1 1 1 1 [9,] 1 1 1 1 1 1 1 1 1 1 [10,] 1 1 1 1 1 1 1 1 1 1 R> x <- as.data.frame(diag(10)) R> x[x == 0] <- 1 Error in [<-.data.frame(*tmp*, x == 0, value = 1) : matrix subscripts not allowed in replacement so, as.matrix() is what you are looking for ;-) Torsten> > i found a way to do this with "for()", > but i think this is the worst way to do it. > > So is there a better way? > > Thomas J. Pesl > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Pesl Thomas wrote:> > i want to replace certain values of a matrix. > i know how to replace them when the values are in a vector. > i tried the same way for a matrix and got this answer: > > la[la==0]<-1This works, if la is a matrix.> Error in [<-.data.frame(*tmp*, la == 0, value = 1) : > matrix subscripts not allowed in replacementAnd this shows us: la is a data.frame. la <- as.matrix(la) la[la==0] <- 1 should do the trick.> i found a way to do this with "for()", > but i think this is the worst way to do it. > > So is there a better way?Regards, Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello, this works well:> tmat <- matrix(rep(0,3*3),ncol=3) > tmat[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0> tmat[tmat==0]<-1 > tmat[,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 On Tue, 29 Aug 2000, Pesl Thomas wrote:> i want to replace certain values of a matrix. > i know how to replace them when the values are in a vector. > i tried the same way for a matrix and got this answer: > > la[la==0]<-1 > Error in [<-.data.frame(*tmp*, la == 0, value = 1) : > matrix subscripts not allowed in replacementmaybe you don't really use a matrix? Jan Goebel -- ----------------------------------- Jan Goebel (mailto:jgoebel at diw.de) DIW German Institute for Economic Research SOEP K?nigin-Luise-Str. 5 D-14195 Berlin Germany phone: 49 30 89789-377 ----------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._