如何在R?
中完成复杂(即,不是等连接)例如,假设我们有以下数据:
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)
然后在SQL中可以做到这样的事情:
SELECT A.id, SUM(B.x)
FROM A
JOIN B ON B.m BETWEEN A.m0 AND A.m1
GROUP BY A.id
如何在R中做一些类似的事情(除了使用循环)?
答案 0 :(得分:2)
一种方法是使用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')