我有以下数据框:
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)申请这样做。任何提示都表示赞赏!
作为输出,我希望有一个数据帧,每1列保存2列的总和。 data.frame现在有24列,所以最后我需要12列。
答案 0 :(得分:2)
你可以试试这个:
t(rowsum(t(all), gl(ncol(all)/2, 2)))
HTH