sql - 如何使用 R 中其他数据帧的列查询数据帧

标签 sql r dataframe sqldf

我在 R 中有 2 个数据帧,我想使用数据帧“y”(类似于数据帧“x”的参数)进行查询。

我有这个代码:

x <- c('The book is on the table','I hear birds outside','The electricity 
came back')
x <- data.frame(x)
colnames(x) <- c('text')
x

y <- c('book','birds','electricity')
y <- data.frame(y)
colnames(y) <- c('search')
y

r <- sqldf("select * from x where text IN (select search from y)")
r

我想在这里使用“like”,但我不知道。 你能帮我吗?

最佳答案

如果您想要一个 sqldf 解决方案,我认为这会起作用:

sqldf("select x.text, y.search FROM x JOIN y on x.text LIKE '%' || y.search || '%'")

##                          text      search
## 1    The book is on the table        book
## 2        I hear birds outside       birds
## 3 The electricity \ncame back electricity

关于sql - 如何使用 R 中其他数据帧的列查询数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45638726/

相关文章:

Java Hibernate JPQL 查询(聚合函数 : count)

r - 在 data.table 中删除行后移动数字集

javascript - r- 如何一直显示 highcharter 对象上的标签

R - 向量中特定值的嵌套循环

python - 在 Pandas DataFrame 中查找数字最接近的行

python - 为什么在分配给数据帧转置时我们会丢失数据?

mysql - 用户媒体的数据库设计

带约束的 Mysql 查询出现错误

c# - 定期运行 C# 应用程序以更新数据库

python - 将 pandas 数据帧转换为元组列表 - ('Row' 、 'Column' 、值)