<p>It's worth learning how to use <code>dplyr</code> for this sort of thing:</p>
<pre><code>dat %&gt;% 
    dplyr::group_by(SampleID) %&gt;% 
    dplyr::summarise(sample_mean = mean(X.1))
</code></pre>
<pre><code># A tibble: 4 x 2
  SampleID          sample_mean
  &lt;chr&gt;                   &lt;dbl&gt;
1 LP6008460-DNA_A04   0.0000416
2 s15                 0.0000319
3 s30                 0.0000449
4 s67                 0.0000288
</code></pre>

CC BY-SA 4.0
2020-12-05T19:36:00.233Z
Sorry for each sample should be sum not mean
Zizogolu
CC BY-SA 4.0
2020-12-05T20:50:47.577Z
I think you can make that tiny change yourself, @Exhausted
Ram RS
CC BY-SA 4.0
2020-12-05T21:06:21.090Z
Thank you @RamRS I replaced mean with sum myself but did not make sense
Zizogolu
CC BY-SA 4.0
2020-12-05T22:59:15.747Z
What do you mean by "did not make sense"?
Ram RS
CC BY-SA 4.0
2020-12-05T14:37:05.723Z
2020-12-05T14:37:05.723Z
user438383
<p>I have a dataframe with 27 samples and for each of them I have different number of values</p>
<p>I want to sum up the values for each sample, leaving each sample with a single value</p>
<pre><code>&gt; dput(d[1:10,c(1,8)])
structure(list(SampleID = c(&quot;LP6008460-DNA_A04&quot;, &quot;LP6008460-DNA_A04&quot;, 
&quot;s30&quot;, &quot;s30&quot;, &quot;s67&quot;, &quot;s15&quot;, &quot;s67&quot;, &quot;s67&quot;, &quot;s67&quot;, &quot;s30&quot;), X.1 = c(8.17407e-05, 
1.40067e-06, 3.86017e-05, 9.50247e-05, 7.08333e-07, 3.19133e-05, 
3.1602e-05, 5.00513e-05, 3.29613e-05, 1.003e-06)), row.names = c(NA, 
10L), class = &quot;data.frame&quot;)
&gt; 
</code></pre>
<p>I started to do that manually but that was a pain and inaccurate</p>

CC BY-SA 4.0
2020-12-05T14:38:10.377Z
I have answered this question, but it seems to be more suitable for stack overflow since it's just a programming question without any bioinformatics in. And it's also best to at least pretend you've made an effort to answer the question before, since this is a very common question and already has many answers.
user438383
<r>
Summing up one column entries for each sample
# Summing up one column entries for each sample

(Asked by: username_0 on 2020-12-05)

I have a dataframe with 27 samples and for each of them I have different number of values
I want to sum up the values for each sample, leaving each sample with a single value
> dput(d[1:10,c(1,8)])
structure(list(SampleID = c("LP6008460-DNA_A04", "LP6008460-DNA_A04", 
"s30", "s30", "s67", "s15", "s67", "s67", "s67", "s30"), X.1 = c(8.17407e-05, 
1.40067e-06, 3.86017e-05, 9.50247e-05, 7.08333e-07, 3.19133e-05, 
3.1602e-05, 5.00513e-05, 3.29613e-05, 1.003e-06)), row.names = c(NA, 
10L), class = "data.frame")
> 

I started to do that manually but that was a pain and inaccurate

**Tags:** r
> **Comments:**
> **username_1**: I have answered this question, but it seems to be more suitable for stack overflow since it's just a programming question without any bioinformatics in. And it's also best to at least pretend you've made an effort to answer the question before, since this is a very common question and already has many answers.  

---

## 1 answers

### Answer by username_1 on 2020-12-05. Score: 2

It's worth learning how to use dplyr for this sort of thing:
dat %>% 
    dplyr::group_by(SampleID) %>% 
    dplyr::summarise(sample_mean = mean(X.1))

# A tibble: 4 x 2
  SampleID          sample_mean
                     
1 LP6008460-DNA_A04   0.0000416
2 s15                 0.0000319
3 s30                 0.0000449
4 s67                 0.0000288

> **Comments:**
> **username_0**: Sorry for each sample should be sum not mean  
> **username_2**: I think you can make that tiny change yourself, @Exhausted  
> **username_0**: Thank you @RamRS I replaced mean with sum myself but did not make sense  
> **username_2**: What do you mean by "did not make sense"?  

---

