简体   繁体   English

使用sqldf可以将不同data.frame中列的行子集

[英]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.

相关问题 对具有许多 ID 的子集数据使用 r sqldf select LIKE - Use r sqldf select LIKE for subset data with many IDs R中的数据框像SQL一样使用,可能使用sqldf() - Data Frame in R use like SQL, possibly using sqldf() 如何有条件地使用R中data.frame中的值更新数据库中的行 - How to update rows in Database with values from data.frame in R conditionally 使用 SQLDF 到 select 列中的特定值 - Using SQLDF to select specific values from a column 如何通过 pyspark 中的列向另一个数据帧中的数据帧添加行 - how to add rows to a data frame that are in another data frame by a column in pyspark 如何根据某个列合并来自不同行的某些数据? - How to combine certain data from different rows based on a certain column? 从同一行中相同ID但在SQL中不同列的不同行中移动数据 - Move the data from different rows with same ID in same rows but different column in sql SQL - 使用来自同一表的不同子集中的行的值更新表子集中的值 - SQL - Updating values in subset of table using values from rows in different subset of same table 从data.frame拆分值并创建其他行以存储其组件 - Split value from a data.frame and create additional row to store its component 如果列数据不同,则获取组的行 - Get rows of group if column data is different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM