[英]Use sqldf to subset rows from column in a different data.frame
I'm translating some data.frame code into SQL by using sqldf. 我正在使用sqldf将一些data.frame代码转换为SQL。 My goal here is to subset rows of a data.frame A using a column from B. Is this possible when A and B don't share any column names?
我的目标是使用B中的列对data.frame A的行进行子集化。当A和B不共享任何列名时,这可能吗?
A = data.frame(a1 = c(1:4), a2 = c(101:104))
B = data.frame(b1 = c(1:2), b2 = c(55,56))
A[A$a1 %in% B$b1,]
## a1 a2
## 1 1 101
## 2 2 102
I can subset A if I already know the values from B$b1, but that's not very scalable. 如果我已经知道B $ b1中的值,则可以对A进行子集设置,但这不是很可扩展。
sqldf("select * from A where a1 in (1,2)")
Do I need an inner join and/or is it required to have identical column names? 我是否需要内部联接和/或需要具有相同的列名?
We use paste twice. 我们使用两次粘贴。 To concatenate the elements of the vector
B$b1
separated by commas. 连接由逗号分隔的向量
B$b1
的元素。 And then to concatenate the final text string desired: [1] "select * from A where a1 in( 1,2 )"
然后连接所需的最终文本字符串:
[1] "select * from A where a1 in( 1,2 )"
sqldf(paste("select * from A where a1 in(", paste(B$b1, collapse = ","), ")"))
Output: 输出:
a1 a2
1 1 101
2 2 102
Try this: 尝试这个:
fn$sqldf(" select * from A where a1 in ( `toString(B$b1)` ) ")
or 要么
sqldf("select A.* from A join B on A.a1 = B.b1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.