[R] Count number of rows in a matrix with a character pattern

Rui Barradas ruipbarradas at sapo.pt
Mon Apr 30 00:00:13 CEST 2012


Right.


Michael Weylandt wrote
> 
> 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@> 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@ 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@ 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.
> 

Misread the op. Didn't see 'count', my code gives where, not how many.
So the corrected version shoulld be

sum(grepl("AY", rnames))

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Count-number-of-rows-in-a-matrix-with-a-character-pattern-tp4596848p4597035.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list