简体   繁体   中英

Match with multiple criteria without loop in R

I have a data frame displaying a set of conditions, for example:

B = data.frame(col1 = 1:10, col2 = 11:20 )

eg, 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.

Solution in base R

# Sum identical rows
A.summed <- aggregate(col3 ~ col1 + col2, data = A, sum);

# Select col1 col2 combinations that are also present in B 
A.summed.sub <- subset(A.summed, paste(col1, col2) %in% paste(B$col1, B$col2));
#   col1 col2 col3
#1     1   11  213
#2     2   12  215
#3     3   13  217
#4     4   14  219
#5     5   15  221
#6     6   16  223
#7     7   17  225
#8     8   18  227
#9     9   19  229
#10   10   20  231

Or the same as a one-liner

A.summed.sub <- subset(aggregate(col3 ~ col1 + col2, data = A, sum), paste(col1, col2) %in% paste(B$col1, B$col2));

# Add summed col3 to dataframe B by matching col1 col2 combinations
B$col3 <- A.summed[match(paste(B$col1, B$col2), paste(A.summed$col1, A.summed$col2)), "col3"];
B;
#   col1 col2 col3
#1     1   11  213
#2     2   12  215
#3     3   13  217
#4     4   14  219
#5     5   15  221
#6     6   16  223
#7     7   17  225
#8     8   18  227
#9     9   19  229
#10   10   20  231

A solution using dplyr . A2 is the final output. The idea is grouping the value in col1 and col2 and calculate the sum for col3 . semi_join is to filter the data frame by matching values based on col1 and col2 in B .

library(dplyr)

A2 <- A %>%
  group_by(col1, col2) %>%
  summarise(col3 = sum(col3)) %>%
  semi_join(B, by = c("col1", "col2")) %>%
  ungroup()
A2
# # A tibble: 10 x 3
#     col1  col2  col3
#    <int> <int> <int>
#  1     1    11   213
#  2     2    12   215
#  3     3    13   217
#  4     4    14   219
#  5     5    15   221
#  6     6    16   223
#  7     7    17   225
#  8     8    18   227
#  9     9    19   229
# 10    10    20   231

We can do a join on using data.table

library(data.table(
setDT(A)[B, .(col3 = sum(col3)), on = .(col1, col2), by = .EACHI]
#    col1 col2 col3
# 1:    1   11  213
# 2:    2   12  215
# 3:    3   13  217
# 4:    4   14  219
# 5:    5   15  221
# 6:    6   16  223
# 7:    7   17  225
# 8:    8   18  227
# 9:    9   19  229
#10:   10   20  231

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM