I have a data frame like the following:
a b c d e
1 2 3 1 1
2 3 0 1 1
3 4 2 1 1
4 5 0 1 2
5 0 7 1 2
I would like to sum each column for each unique value in column e
.
So my output should be:
a b c d e
6 9 5 3 1
9 5 7 2 2
How can I do it?
I have a data frame like the following:
a b c d e
1 2 3 1 1
2 3 0 1 1
3 4 2 1 1
4 5 0 1 2
5 0 7 1 2
I would like to sum each column for each unique value in column e
.
So my output should be:
a b c d e
6 9 5 3 1
9 5 7 2 2
How can I do it?
With by
cbind(do.call(rbind, by(d[-5], d$e, colSums)), e=unique(d$e))
# a b c d e
# 1 6 9 5 3 1
# 2 9 5 7 2 2
or use aggregate
aggregate(. ~ e, d, FUN=sum)
# e a b c d
# 1 1 6 9 5 3
# 2 2 9 5 7 2
Data
d <- structure(list(a = 1:5, b = c(2L, 3L, 4L, 5L, 0L), c = c(3L,
0L, 2L, 0L, 7L), d = c(1L, 1L, 1L, 1L, 1L), e = c(1L, 1L, 1L,
2L, 2L)), row.names = c(NA, -5L), class = "data.frame")