Dear all, How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0 Could someone please help me with that? Thanks in advance! Sincerely, Tord Sn?ll ----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/snall.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
replace() in matrix
6 messages · Tord Snall, Guido Masarotto, Uwe Ligges +2 more
On Mon, Jul 02, 2001 at 06:51:40PM +0200, Tord Snall wrote:
How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0 Could someone please help me with that?
It is explained in the "An introduction to R" manual (which is included in the main R distribution) under "Index vectors: selecting and modifying subsets of a data set" An example is
a <- matrix(c(1,24,1,103,56,1,1,1,27),3,3) a
[,1] [,2] [,3] [1,] 1 103 1 [2,] 24 56 1 [3,] 1 1 27
a[a<=1] <- 0 a
[,1] [,2] [,3] [1,] 0 103 0 [2,] 24 56 0 [3,] 0 0 27
guido -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Tord Snall wrote:
Dear all, How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0
A[A == 1] <- 0 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Probably not the most elegant, but if you have tmp <- matrix(like below) (1-(tmp==1))*tmp should give you the desired result. -Ryan Elmore PSU - Dept of Statistics
On Mon, 2 Jul 2001, Tord Snall wrote:
Dear all, How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0 Could someone please help me with that? Thanks in advance! Sincerely, Tord Snäll ----------------------------------------------------------------------- Tord Snäll Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villavägen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/snall.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear all, Thanks to Douglas Bates, Uwe Ligges, Andy Liaw, Giovanni Petris, Ryan Elmore, Mark Myatt and Guido Masarotto for answering my question on replacing cells in a matrix! Guido Masarotto even pointed me to the solution in An introduction to R. I had searched only in R for beginners. As a beginner, non-statisticians and only R user at my dept., I really appreciate the help from you all. Sincerely, Tord Sn?ll
At 18:51 2001-07-02 +0200, you wrote:
Dear all, How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0 Could someone please help me with that? Thanks in advance! Sincerely, Tord Sn?ll ----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/snall.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
._._
----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/snall.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Tord, Writes:
How do I replace values of a matrix, for exemple I want the matrix 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 to be replaced by 0 24 0 0 0 24 0 0 0 0 0 0 0 45 104 0 0 45 0 92 0 0 104 92 0 Could someone please help me with that?
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._