katarv
2012-Apr-29 19:46 UTC
[R] Count number of rows in a matrix with a character pattern
Hi, I have a large data set that I input as a matrix, where I have 1:x rows with names AX, then x+1: y rows named AY, etc. The idea is that I have to count how many rows exactly I have with name AX and how many I have with name AY (or find which row numbers have names AX). Is there any way in R to count a number of rows with a name matching a required pattern? Thanks in advance, Katie -- View this message in context: http://r.789695.n4.nabble.com/Count-number-of-rows-in-a-matrix-with-a-character-pattern-tp4596848p4596848.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Apr-29 21:26 UTC
[R] Count number of rows in a matrix with a character pattern
Hello, katarv wrote> > Hi, > > I have a large data set that I input as a matrix, where I have 1:x rows > with names AX, then x+1: y rows named AY, etc. The idea is that I have > to count how many rows exactly I have with name AX and how many I have > with name AY (or find which row numbers have names AX). Is there any way > in R to count a number of rows with a name matching a required pattern? > > > Thanks in advance, > > Katie >Yes, there are ways of doing what you need. In the example below I'm not assuming the names matching a pattern are consecutive, it's just simpler to define the names like that. # Make an example matrix. ax <- paste("AX", 1:4, sep="") ay <- paste("AY", 1:6, sep="") az <- paste("AZ", 1:2, sep="") mat <- matrix(1:24, ncol=2, dimnames=list(c(ax, ay, az), NULL)) mat # Now get the names and search (grep) the pattern rnames <- rownames(mat) grep("AY", rnames) Two notes: One, see the help pages for ?grep ?regex Second, next time please post an example dataset using 'dput' dput(mat) structure(1:24, .Dim = c(12L, 2L), .Dimnames = list(c("AX1", "AX2", "AX3", "AX4", "AY1", "AY2", "AY3", "AY4", "AY5", "AY6", "AZ1", "AZ2"), NULL)) Like this it's just a matter of copy&paste to our R session. Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Count-number-of-rows-in-a-matrix-with-a-character-pattern-tp4596848p4596993.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Apr-29 21:52 UTC
[R] Count number of rows in a matrix with a character pattern
I'd use a combination of rownames(), grepl() and sum(). Get the names with the first, test with the second and count the positives (by coercing TRUE -> 1) with the last Michael On Apr 29, 2012, at 3:46 PM, katarv <katiasmirn at gmail.com> wrote:> Hi, > > I have a large data set that I input as a matrix, where I have 1:x rows > with names AX, then x+1: y rows named AY, etc. The idea is that I have to > count how many rows exactly I have with name AX and how many I have with > name AY (or find which row numbers have names AX). Is there any way in R to > count a number of rows with a name matching a required pattern? > > > Thanks in advance, > > Katie > > -- > View this message in context: http://r.789695.n4.nabble.com/Count-number-of-rows-in-a-matrix-with-a-character-pattern-tp4596848p4596848.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.