title: R: count the number of entries above a threshold, in at least X samples of the same type?
body: <p>I would like to count the number of row entries above a threshold in at least X samples of the same type (column ID)? Here is an example of what I would like to do:</p>
  
  <p>Count the number of genes (g1 - g4) with counts higher than 5 in at least 2 cultivars of the same type. </p>
  
  <pre><code>Cultivar A A B B B
  g1       5 2 6 7 10
  g2       6 9 3 2 1
  g3       2 1 3 4 5
  g4       4 6 7 3 2 
  </code></pre>
  
  <p>Counting will have to result in counting g1, g2, but not g3 and g4. 
  Result = 2.
  I know how to count the number of genes in two random samples and randomizing and subsampling the process:</p>
  
  <pre><code>#Filter for genes with values larger than 5 reads in at least 2 random samples
  # Replace values &gt;5 to 1
  Gene_counts1=ifelse(RAW_gene_counts&gt;5,1,0)
  # Number of randomizations 100
  MATRIX = matrix(NA,100,length(SAMPLE))
  x=1
  SAMPLE = c(10,20,30,40,50,60)
  for(i in SAMPLE){
  for (j in 1:100){
      columns = sample(1:ncol(Gene_counts1), i, replace = TRUE)
      random_sample = mapply(function(row,col)return(Gene_counts1[row,col]), col=columns)
      MATRIX[j,x] = sum(rowSums(random_sample)&gt;=2)
      }
      x=x+1
  }
  MATRIX
  colnames(MATRIX)=SAMPLE
  </code></pre>
  
  <p>Can anyone help me out?</p>