【发布时间】:2017-03-16 10:16:25
【问题描述】:
我有想要在另一个数据帧上执行的 SQL 查询的数据帧。
queries <- structure(list(Name = c("innovation", "foos", "testing"), A = c("select * from data WHERE `TEXT` RLIKE '[[:<:]]innovat[^[:space:]]+[[:>:]]'", "select * from data WHERE `TEXT` RLIKE '[[:<:]]foo[^[:space:]]+[[:>:]]'", "select * from data WHERE `TEXT` RLIKE '[[:<:]]test[^[:space:]]+[[:>:]]'"), B = c("", "b", "b"), C = c("c", "c", "c")), .Names = c("Name", "Query", "Q1_2", "Q1_3"), row.names = c(NA, -3L), class = "data.frame")
我想使用 sqldf 包遍历这些查询,并将每个查询的数据框输出命名为在数据框查询中找到的对应名称。我还需要在每个数据框中创建一个与数据框名称匹配的新变量。
样本数据集
data <- structure(list(Participant = 1:3, A = c("and other foo things", "testing test and foo", "nothing here"), B = c("", "b", "b"), C = c("c", "c", "c")), .Names = c("Participant", "TEXT", "other", "another"), row.names = c(NA, -3L), class = "data.frame")
类似:
for (i in queries[2]) {
i<- as.data.frame(sqldf(i)
i$category <- i
}
但这还不行。有什么建议吗?
【问题讨论】:
-
错字,已更正为数据数据框