In the following matrix dataset:
1 2 3 4 5
1950 7 20 21 15 61
1951 2 10 6 26 57
1952 12 27 43 37 34
1953 14 16 40 47 94
1954 2 17 62 113 101
1955 3 4 43 99 148
1956 2 47 31 85 79
1957 17 5 38 216 228
1958 11 20 15 76 68
1959 16 20 43 30 226
1960 9 28 28 70 201
1961 1 31 124 74 137
1962 12 25 37 41 200
I have been trying to calculate colSums by decade i.e., find sum the each column from 1950-1959 and then from 1960-69 and so on.
I tried tapply, ddply, etc but couldn't figure out something that would actually work.
Group By Sum in R using dplyr You can use group_by() function along with the summarise() from dplyr package to find the group by sum in R DataFrame, group_by() returns the grouped_df ( A grouped Data Frame) and use summarise() on grouped df results to get the group by sum.
Group By Multiple Columns in R using dplyr Use group_by() function in R to group the rows in DataFrame by multiple columns (two or more), to use this function, you have to install dplyr first using install. packages('dplyr') and load it using library(dplyr) .
The colSums() function in R can be used to calculate the sum of the values in each column of a matrix or data frame in R. where: x: Name of the matrix or data frame. na.
First we set up the matrix used as input.
Lines <- "1 2 3 4 5
1950 7 20 21 15 61
1951 2 10 6 26 57
1952 12 27 43 37 34
1953 14 16 40 47 94
1954 2 17 62 113 101
1955 3 4 43 99 148
1956 2 47 31 85 79
1957 17 5 38 216 228
1958 11 20 15 76 68
1959 16 20 43 30 226
1960 9 28 28 70 201
1961 1 31 124 74 137
1962 12 25 37 41 200 "
DF <- read.table(text = Lines, check.names = FALSE)
m <- as.matrix(DF)
Now, below, we show some alternative solutions. (1) seems the most flexible in that we can easily replace sum
with other functions to get different effects but (2) is the shortest for this particular problem. Also note that there are some slight differences. (1) produces a data.frame while the other two produce a matrix.
1) aggregate
decade <- 10 * as.numeric(rownames(m)) %/% 10
m.ag <- aggregate(m, data.frame(decade), sum)
which gives this data.frame:
> m.ag
decade 1 2 3 4 5
1 1950 86 186 342 744 1096
2 1960 22 84 189 185 538
2) rowsum
This one is shorter. It produces a matrix result.
rowsum(m, decade)
3) split/sapply
. This one produces a matrix as well. if we had DF
we could replace as.data.frame(m) with DF
shortening it slightly.
t(sapply(split(as.data.frame(m), decade), colSums))
EDIT: added solutions (2) and (3) Added some clarifications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With