I'm trying to use the sapply
(or similiar) function to sum all of the values that match multiple criteria throughout the data set.
I was able to write the code for a specific match, but am not sure how to use R to apply to every unique match in the data frame.
For example, if my data frame is constructed with 3 columns
col1 <- c("a", "a", "a", "b", "b", "b", "b", "b", "b")
col2 <- c(1, 1, 1, 2, 2, 2, 1, 1, 1)
col3 <- c(10, 5, 10, 5, 5, 1, 3, 4, 5)
df <- data.frame(col1, col2, col3)
Here is the code I'm using for one match:
tmp <- subset(df, col1 == "a" & col2==1)
sum(tmp[,3])
This code correctly returns 25 for the sum of col3
matching the 2 criteria in the subset
function.
How do I do this calculation for the 3 unique combinations in the data frame? I'm looking for the following output
col1 col2 sum_col3
a 1 25
b 1 12
b 2 11
Thanks for assistance in advance.