Reputation: 1525
How can a complex (i.e., not an equi-join) be done in R?
For example, suppose we have the following data:
B <- data.frame(m = 1:100, x = rnorm(100))
A <- data.frame(id = 1:200, m0 = sample(B$m, 200, replace = TRUE))
A$m1 <- A$m0 + sample(1:20, nrow(A), replace = TRUE)
Then in SQL one could do something like this:
SELECT A.id, SUM(B.x)
FROM A
JOIN B ON B.m BETWEEN A.m0 AND A.m1
GROUP BY A.id
How can one do something analogous in R (aside from using a loop)?
Upvotes: 2
Views: 371
Reputation: 1525
One approach is to use sqldf:
# Using data defined in question
library('sqldf')
R <- sqldf('select A.id, sum(B.x) s from A join B on B.m between A.m0 and A.m1 group by A.id')
Upvotes: 2