I want to be able to collapse and sum values dependent on the gene name

I have a table that looks like this:

I want add together all the values for each gene for each column. For example, for LINC01128, it should read: ConN1 ConN2 ConN3 StN1 StN2 StN3 LINC01128 : 22 14 37 34 54 67

My table is very long and this would need to be done for all the genes.

Topic aggregation r

Category Data Science


data = aggregate(data,
                by = list(gene),
                FUN = sum)

You can use dplyr, where df is your data.frame:

df %>% group_by(gene) %>% summarise_if(is.numeric, sum)

This will sum each column separately (assuming it is numeric), grouped by each gene. Any column that is not included in the group_by or is not numeric will be dropped at this stage.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.