2014-05-06 100 views
1

總和我有以下的數據幀:計算每2列

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") 

在該數據幀我需要每2列在最簡單的形式這是可以做到的總和:所有[1] +所有[2],所有[3] +所有[4]等等,然後在最後我可以再次綁定新的框架,但我現在可以通過類似聚合或應用的方式來完成。只有我還沒有成功。我現在最好的嘗試是:allfinal <-aggregate(all ,FUN = sum,by=[1:2])我知道這不是它應該如何工作,但無法弄清楚如何正確使用聚合或(s)適用於做到這一點。任何提示都表示讚賞!

作爲輸出我想有一個數據框,其中包含2列的總和每1列。 data.frame現在有24列,所以最後我需要12列。

+1

我可能只是做'所有[C(T,F)] +所有[C(F,T)]' – jdharrison

+0

@jdharrison這更好的作品我必須說,因爲在這裏我的數據框保持一個數據框,並在下面的答案是創建一個矩陣 –

回答

2

你可以試試這個:

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

心連心

+0

確實有效!謝謝您的幫助。 –