I have a data frame displaying a set of conditions, for example:
B = data.frame(col1 = 1:10, col2 = 11:20 )
e.g., the first row says that when col1 = 1, col2 = 11. I also have another data frame with the numbers that should met these conditions, for example:
A = data.frame(col1 = c(1:11,1:11), col2 = c(11:21,11:21), col3 = 101:122)
I would like to return the sum of the values in col3 in matrix A for all rows that meat the conditions in B. For example, using the first row in B this value is:
sum(A$col3[which(A$col1 == B$col1[1] & A$col2 == B$col2[1])])
#[1] 213
that is the sum of the entries in col3 in the 1st and 12th row of A. I need to find a vector with all these sums for all rows of matrix A. I know how to do this with a loop, however in my data matrices A and B are very large and have many conditions, so I was wondering whether there is a way to do the same thing without the loop. Thank you.