# How can I count only the sublists having more than 9 number of '2'?

I made a table giving me 1's and 2's

In[23]:= S = Join[Table[1, 1], Table[2, 1]]


Out[23]= {1, 2}

And then I made it bigger:

T = RandomChoice[S, 15*365];


And then I separated it:

R = Partition[T, 15];


It looks something like this:

{{2, 1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1}, {2, 2, 2, 2, 1, 1, 2,
1, 2, 2, 1, 2, 1, 1, 2}, {2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2,
2}, {2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1}, {1, 1, 1, 1, 1,
2, 2, 2, 1, 2, 2, 2, 2, 1, 1}, {2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 1,
1, 1, 1}, {1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}, {1, 1, 1,
2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 2, 2}, {2, 1, 2, 2, 1, 2, 2, 1, 2, 2,
1, 2, 2, 2, 2}, {2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2}, {1,
2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1}, {1, 1, 2, 1, 2, 1, 2, 2,
1, 2, 1, 1, 2, 1, 2}, {2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 2,
1}, {2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1}, {2, 2, 1, 1, 1,
1, 2, 2, 1, 2, 1, 1, 2, 1, 1}, {1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1,
1, 2, 2},


but much bigger. I want to count the number of sublists with more than 9 2's only. Is there anyway to do this? Is there a way to treat the sublists as a separate unit, as opposed to the entire list of R? Thanks.

• Don't use uppercase initials for your symbols: you will eventually end up clashing with some built-in. Tr[UnitStep[Total[R - 1, {2}] - 10]] will quickly do what you want.
– ciao
Nov 7, 2019 at 21:36
• Jus use this for R= RandomChoice[{1,2}, {365,15}]; Nov 8, 2019 at 5:50

This might work for you ... let your list be called r, then try

Count[Map[Count[#, 2] > 9 &, r], True]


If you need further explanation, ask for it.

• Yes it worked, thank you! Nov 7, 2019 at 21:51
• Slightly simpler: Count[r, x_ /; Count[x, 2] > 9] Nov 7, 2019 at 21:55
• Also Length[Select[r, Count[#, 2] > 9 &]] Nov 8, 2019 at 1:37

Just another way for this specific context:

r = Partition[RandomChoice[{1, 2}, 15 365], 15];
Count[Total /@ (r - 1), _?(# > 9 &)]


However, as comments illustrate there are many ways to do this.