2

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?

Violet
  • 21
  • 1
  • 1
    Assuming the input data is arranged with subtypes in separate rows, then `rowSums(M > x)` would seem to be the obvious first attempt. – IRTFM Feb 16 '22 at 22:52
  • The input data is arranged in a matrix where the columns are grouped into subtypes and each row is unique but of course follows the same pattern of subtypes. – Violet Feb 16 '22 at 23:17
  • I'm pretty sure my suggestion would work with your example. I cannot tell from you brief description whether it would work with your current situation. Do different subtypes have varying numbers of rows? – IRTFM Feb 16 '22 at 23:58

1 Answers1

1
 M <- data.matrix( read.table(text="      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", head=TRUE))

 x=50

Assuming the use of the word "matrix" is referring to an R matrix. then this demonstrates the comment suggestion on the example.

 rowSums(M > x)
# A  B  C  D  E 
#17 17 17 17  0 
IRTFM
  • 258,963
  • 21
  • 364
  • 487