Question

I have the following data frame:

all <- structure(list(counts = c(0L, 0L, 3L, 0L, 2L, 0L), counts = c(0L, 
2L, 1L, 0L, 5L, 1L), counts = c(1L, 9L, 17L, 0L, 7L, 2L), counts = c(2L, 
1L, 13L, 0L, 7L, 5L), counts = c(1L, 1L, 3L, 0L, 2L, 10L), counts = c(0L, 
2L, 2L, 0L, 8L, 9L), counts = c(0L, 4L, 4L, 0L, 4L, 0L), counts = c(0L, 
2L, 3L, 0L, 7L, 1L), counts = c(0L, 2L, 0L, 0L, 3L, 8L), counts = c(1L, 
3L, 3L, 0L, 4L, 13L), counts = c(0L, 6L, 12L, 0L, 3L, 2L), counts = c(0L, 
7L, 6L, 0L, 4L, 2L), counts = c(1L, 0L, 1L, 0L, 2L, 5L), counts = c(1L, 
1L, 2L, 0L, 3L, 6L), counts = c(0L, 2L, 1L, 1L, 2L, 0L), counts = c(0L, 
4L, 1L, 0L, 4L, 0L), counts = c(0L, 2L, 1L, 0L, 3L, 3L), counts = c(0L, 
1L, 1L, 0L, 2L, 1L), counts = c(0L, 3L, 1L, 0L, 5L, 0L), counts = c(0L, 
4L, 5L, 0L, 1L, 0L), counts = c(0L, 2L, 5L, 0L, 8L, 23L), counts = c(0L, 
0L, 2L, 0L, 1L, 7L), counts = c(1L, 0L, 0L, 0L, 1L, 2L), counts = c(0L, 
0L, 0L, 0L, 1L, 0L)), .Names = c("counts", "counts", "counts", 
"counts", "counts", "counts", "counts", "counts", "counts", "counts", 
"counts", "counts", "counts", "counts", "counts", "counts", "counts", 
"counts", "counts", "counts", "counts", "counts", "counts", "counts"
), row.names = c("1/2-SBSRNA4", "A1BG", "A1BG-AS1", "A1CF", "A2LD1", 
"A2M"), class = "data.frame")

In this dataframe i need the sum of every 2 columns in the simplest form this can be done with: all[1] + all[2], all[3] + all[4] etc etc. then at the end i could cbind the new frames again but i now this can be done with something like aggregate or apply. Only i did not yet manage to succeed. My best try now is: allfinal <-aggregate( all ,FUN = sum,by=[1:2] ) I know this is not how it should work but cant figure out how to correctly use aggregate or (s)apply to do this. Any tips are appreciated!

As output i want to have a dataframe that holds the sum of 2 columns per 1 columns. The data.frame now has 24 columns so at the end i need 12 columns.

Was it helpful?

Solution

you can try this:

 t(rowsum(t(all), gl(ncol(all)/2, 2)))

hth

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top