0

Given a data frame containing 4 columns (my data frame contains approx. 40 genes)

my_df <- read.table(header=T, text="
count   gene    group   sample
0.1 gene1   exp S_1
0.2 gene1   exp S_2
0.5 gene2   cnt S_1
0.6 gene2   exp S_1
0.2 gene2   exp S_2
0.4 gene1   cnt S_1
0.3 gene1   cnt S_2
0.2 gene1   cnt S_3

I want to collapse count value for each gene and collapse by group (exp and cnt) This is the desired output as a data frame:

count   gene    group
0.3 gene1   exp
0.9 gene1   cnt
0.8 gene2   exp
0.5 gene2   cnt

Some specification: Colum "sample" can be removed since it is only to display that number of gene appearance for each group in unpair. Thanks a lot for your suggestions

RebiKirl
  • 85
  • 7

1 Answers1

1
library(dplyr)
my_df %>% group_by(gene, group) %>% 
    summarise(count = sum(count))
h1427096
  • 248
  • 1
  • 9