I know there are a lot of similar questions on how to sum up a column under a condition in R. But I somehow cannot implement the function aggregate
or dplyr::group_by(df) %>% summarise(variable = sum(variable))
in my data. Also Combine rows and sum their values does not help me. But maybe you can?
I want to merge and sum up rows of a data.frame in R.
df <- data.frame(file=c('sample1','sample1','sample2','sample3','sample2'),gene1=c(34,365,76,0,4),gene2=c(34,0,0,456,0))
> df
file gene1 gene2
1 sample1 34 34
2 sample1 365 0
3 sample2 76 0
4 sample3 0 456
5 sample2 4 0
The output should look like this
file gene1 gene2
1 sample1 399 34
2 sample2 80 0
3 sample3 0 456