-4
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ – user438383 Dec 5 '20 at 14:38
2
$\begingroup$

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
  <chr>                   <dbl>
1 LP6008460-DNA_A04   0.0000416
2 s15                 0.0000319
3 s30                 0.0000449
4 s67                 0.0000288
$\endgroup$
4
  • $\begingroup$ Sorry for each sample should be sum not mean $\endgroup$ – Exhausted Dec 5 '20 at 19:36
  • 3
    $\begingroup$ I think you can make that tiny change yourself, @Exhausted $\endgroup$ – Ram RS Dec 5 '20 at 20:50
  • $\begingroup$ Thank you @RamRS I replaced mean with sum myself but did not make sense $\endgroup$ – Exhausted Dec 5 '20 at 21:06
  • 2
    $\begingroup$ What do you mean by "did not make sense"? $\endgroup$ – Ram RS Dec 5 '20 at 22:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.