Answers.Body: <p><code>apply(my_df &gt;= 10, 1, sum)</code> would give you that how many of the samples satisfy your criteria. Basically you <code>apply()</code> the function <code>sum()</code> to the rows (denoted as <code>1</code>). The <code>sum()</code> does the trick that @Devon Ryan suggested above.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-05T17:22:24.923Z
Answers.Text: I get all zero this is weird  > apply(df >= 5, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  > apply(df >= 1, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  >
Answers.UserDisplayName: Mahta Mira
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-05T17:26:30.343Z
Answers.Text: Check your `df`, looks like you have manipulated that data frame.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-05T17:35:03.400Z
Answers.Text: No just df=t(oc) I used in the code
Answers.UserDisplayName: Mahta Mira
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-05T17:14:18.113Z
Answers.LastActivityDate: 2021-08-05T17:14:18.113Z
Answers.UserDisplayName: haci
Body: <p>I have a data frame of raw read counts, genes in rows and samples in columns</p>
  <pre><code>&gt; dput(head(oc))
  structure(list(sample1 = c(13L, 0L, 1L, 42L, 15L, 1137L), sample2 = c(21L, 
  2L, 20L, 51L, 5L, 203L), sample3 = c(25L, 2L, 6L, 71L, 36L, 1346L
  ), sample4 = c(59L, 3L, 8L, 58L, 42L, 3428L), sample5 = c(19L, 
  4L, 8L, 52L, 1L, 201L)), row.names = c(&quot;WASH7P&quot;, &quot;MIR6859-1&quot;, 
  &quot;CICP27&quot;, &quot;WASH9P&quot;, &quot;MTND1P23&quot;, &quot;MTND2P28&quot;), class = &quot;data.frame&quot;)
  &gt; 
  
  &gt; head(oc)
            sample1 sample2 sample3 sample4 sample5
  WASH7P         13      21      25      59      19
  MIR6859-1       0       2       2       3       4
  CICP27          1      20       6       8       8
  WASH9P         42      51      71      58      52
  MTND1P23       15       5      36      42       1
  MTND2P28     1137     203    1346    3428     201
  &gt; 
  </code></pre>
  <p>I just want to keep genes with at least 10 reads in each sample, I mean even if a gene has less than 10 reads in one sample and the rest of four samples have 10 reads, this gene should be removed. For instance here the second, third and fifth rows should be removed</p>
  <p>I tied this but all I obtained zero</p>
  <pre><code>&gt; df=t(oc)
  &gt; cutoff &lt;- 10
  &gt; 
  &gt; # encode the data based on the cutoff
  &gt; df[df &gt;= cutoff] &lt;- 1
  &gt; df[df &lt; cutoff] &lt;- 0
  &gt; View(df)
  &gt; rowSums(df)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  &gt; 
  
  &gt; apply(df &gt;= 5, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  &gt; apply(df &gt;= 1, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  &gt; 
  </code></pre>
  <p>Any suggestion please?</p>
  
Tags: <r><rna-seq>
Title: Keeping rows with a cut-off in all columns
ThreadText: # Keeping rows with a cut-off in all columns
  
  (Asked by: username_0 on 2021-08-05)
  
  I have a data frame of raw read counts, genes in rows and samples in columns
  
  ```
  > dput(head(oc))
  structure(list(sample1 = c(13L, 0L, 1L, 42L, 15L, 1137L), sample2 = c(21L, 
  2L, 20L, 51L, 5L, 203L), sample3 = c(25L, 2L, 6L, 71L, 36L, 1346L
  ), sample4 = c(59L, 3L, 8L, 58L, 42L, 3428L), sample5 = c(19L, 
  4L, 8L, 52L, 1L, 201L)), row.names = c("WASH7P", "MIR6859-1", 
  "CICP27", "WASH9P", "MTND1P23", "MTND2P28"), class = "data.frame")
  > 
  
  > head(oc)
            sample1 sample2 sample3 sample4 sample5
  WASH7P         13      21      25      59      19
  MIR6859-1       0       2       2       3       4
  CICP27          1      20       6       8       8
  WASH9P         42      51      71      58      52
  MTND1P23       15       5      36      42       1
  MTND2P28     1137     203    1346    3428     201
  > 
  
  ```
  
  I just want to keep genes with at least 10 reads in each sample, I mean even if a gene has less than 10 reads in one sample and the rest of four samples have 10 reads, this gene should be removed. For instance here the second, third and fifth rows should be removed
  
  I tied this but all I obtained zero
  
  ```
  > df=t(oc)
  > cutoff <- 10
  > 
  > # encode the data based on the cutoff
  > df[df >= cutoff] <- 1
  > df[df < cutoff] <- 0
  > View(df)
  > rowSums(df)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  > 
  
  > apply(df >= 5, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  > apply(df >= 1, 1, sum)
  sample1 sample2 sample3 sample4 sample5 
        0       0       0       0       0 
  > 
  
  ```
  
  Any suggestion please?
  
  **Tags:** r, rna-seq
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-05. Score: 1
  
  `apply(my_df >= 10, 1, sum)` would give you that how many of the samples satisfy your criteria. Basically you `apply()` the function `sum()` to the rows (denoted as `1`). The `sum()` does the trick that @Devon Ryan suggested above.
  
  > **Comments:**
  > **username_0**: I get all zero this is weird  > apply(df >= 5, 1, sum) sample1 sample2 sample3 sample4 sample5        0       0       0       0       0  > apply(df >= 1, 1, sum) sample1 sample2 sample3 sample4 sample5        0       0       0       0       0  >  
  > **username_1**: Check your `df`, looks like you have manipulated that data frame.  
  > **username_0**: No just df=t(oc) I used in the code  
  
  ---
  
  