Skip to content
Prev 12101 / 398502 Next

replace() in matrix

Tord,

Writes:
Use an index:

        your.mat.data <- c(1, 24, 1, 1, 1, 24, 1, 1, 1, 1, 1, 1, 1, 45,
                           104, 1, 1, 45, 1, 92, 1, 1, 104, 92, 1)

        your.mat <- matrix(data = your.mat.data, 
                           nrow = 5,
                           ncol = 5,
                           byrow = TRUE)

        your.mat

        your.mat[your.mat == 1] <- 0

        your.mat

The index is [your.mat == 1].

An alternative is to use the replace() function:

        your.mat.data <- c(1, 24, 1, 1, 1, 24, 1, 1, 1, 1, 1, 1, 1, 45,
                           104, 1, 1, 45, 1, 92, 1, 1, 104, 92, 1)

        your.mat <- matrix(data = your.mat.data, 
                           nrow = 5,
                           ncol = 5,
                           byrow = TRUE)

        your.mat

        your.mat <- replace(your.mat, your.mat == 1, 0)

Mark


--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._