Hi, I wonder if there is any function which gives the number of times which an element is repeated in the matrix. Let say there is a matrix, I would like to find out how many times number 2 has been repeated in the matrix, or in other words, how many elements of the matrix are equal 2. Thanks for your help, Rostam [[alternative HTML version deleted]]
Try this: mat <- matrix(sample(1:50, 100, rep = TRUE), 10) table(mat)[which(table(mat) == 2)] On 8/11/08, rostam shahname <rostamepython at gmail.com> wrote:> Hi, I wonder if there is any function which gives the number of times which > an element is repeated in the matrix. Let say there is a matrix, I would > like to find out how many times number 2 has been repeated in the matrix, or > in other words, how many elements of the matrix are equal 2. > Thanks for your help, > Rostam > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
You can use the length() and which() functions for that: > length(which(m == 2)) cheers, brandon rostam shahname wrote:> Hi, I wonder if there is any function which gives the number of times which > an element is repeated in the matrix. Let say there is a matrix, I would > like to find out how many times number 2 has been repeated in the matrix, or > in other words, how many elements of the matrix are equal 2. > Thanks for your help, > Rostam > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi, Brandon Invergo wrote:> You can use the length() and which() functions for that: > > > length(which(m == 2))Or even shorter since TRUE Values have a value of 1 mymatrix <- matrix(sample(x=1:10, size=1000, replace=TRUE), ncol=5) sum(mymatrix==2) length(which(mymatrix==2)) #should give the same result Best, Roland
How about x = matrix(sample(1:10, 100, replace=T), ncol=10) table(as.vector(x)) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of rostam shahname Sent: Monday, August 11, 2008 1:42 PM To: r-help at r-project.org Subject: [R] number of an element in a matrix Hi, I wonder if there is any function which gives the number of times which an element is repeated in the matrix. Let say there is a matrix, I would like to find out how many times number 2 has been repeated in the matrix, or in other words, how many elements of the matrix are equal 2. Thanks for your help, Rostam [[alternative HTML version deleted]] ______________________________________________ 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.
My last email went out faster than I could think. Actually no need to coerce a matrix to a vector. x = matrix(sample(1:10, 100, replace=T), ncol=10) table(x) H -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Horace Tso Sent: Monday, August 11, 2008 2:37 PM To: rostam shahname; r-help at r-project.org Subject: Re: [R] number of an element in a matrix How about x = matrix(sample(1:10, 100, replace=T), ncol=10) table(as.vector(x)) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of rostam shahname Sent: Monday, August 11, 2008 1:42 PM To: r-help at r-project.org Subject: [R] number of an element in a matrix Hi, I wonder if there is any function which gives the number of times which an element is repeated in the matrix. Let say there is a matrix, I would like to find out how many times number 2 has been repeated in the matrix, or in other words, how many elements of the matrix are equal 2. Thanks for your help, Rostam [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________ 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.
Hi r-help-bounces at r-project.org napsal dne 11.08.2008 22:50:08:> You can use the length() and which() functions for that: > > > length(which(m == 2))Or simply sum(m == 2) Regards Petr> > cheers, > brandon > > > rostam shahname wrote: > > Hi, I wonder if there is any function which gives the number of timeswhich> > an element is repeated in the matrix. Let say there is a matrix, Iwould> > like to find out how many times number 2 has been repeated in thematrix, or> > in other words, how many elements of the matrix are equal 2. > > Thanks for your help, > > Rostam > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.