I have a matrix (49 rows x 533 columns) and the columns are subsetted into 5 "subtypes".
For each row, I want to count how many values are greater than 1 within each subtype
e.g. if I have subsets A,B,C,D,E: "In row (i) how many of the values in subset A are greater than 1?" and the same for b,c,d and e for every row.
Using tapply()
and length()
I am able to count the values for each row by subtype:
lengthBySubtype <- function(x) {tapply(x,subtypes,length)}
apply(dataMatrix,1,lengthBySubtype)
My code returns, for each row, the number of values in each subset. Here's a small chunk of the results:
r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17
A 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111
B 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74
C 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195
D 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
E 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
It's in the exact format I want, but what if I only want to count values that meet a certain condition? (e.g. are greater than 1 in my case). Is there a different function that would work with the apply family for this?