【问题标题】:How do I query sqldf in R using Function parameter/arguments [duplicate]如何使用函数参数/参数在 R 中查询 sqldf [重复]
【发布时间】:2018-12-19 03:41:48
【问题描述】:

我正在尝试以下代码:

df_rhs<- sqldf("select rhs from df_basket12 where lhs like '%", med, "%'")

这里,med函数的函数参数

med_aff1<- function(med)

错误来了:

result_create(conn@ptr, statement) 中的错误:无法识别的令牌:“'%”

【问题讨论】:

    标签: r sqldf


    【解决方案1】:

    您可以简单地粘贴来连接字符串。

    med <- "some text"
    command <- paste0("select rhs from df_basket12 where lhs like '%", med, "%'")
    df_rhs<- sqldf(command)
    

    附录:

    我不确定你的函数应该做什么......但是存储在 med 变量中的输出应该是作为 SQL 语句的一部分有效的字符串。

    附录2:

    如果您想遍历多个元素,您需要弄清楚如何处理不同查询的不同输出。由于您没有说明明确的方向,我将它们存储在一个列表中。

    med <- c("a", "b") #this would be your multiple element vector
    
    results=list()  #init an empty list
    
    #loop through the vector
    
    for (i in (med)) {
    
      command <- paste0("select rhs from df_basket12 where lhs like '%", i, "%'")
      results[i] <- sqldf(command)
    }
    

    【讨论】:

    • 谢谢你它工作正常。但如果你能帮助我,另一个问题就是那里。 'med' 是具有 {A,B} 的列表/数组。当我使用上述查询时,它会显示 {A,B} 而不是 {B,A} 的 rhs 规则。我该怎么办?
    • @KoushikRoy 我刚刚更新了答案
    • 谢谢。但是,我认为这会导致“A”药物和“B”药物的匹配。但我想要 set{A,B} 和 set{B,A} 的匹配项。我将这个“医学”作为 API 发送……假设。 med=A,B 现在,我得到的输出是 {A},{B},{A,B},{B,A} 的所有可能结果。我只需要 {A,B} 的 rhs 和{B,A}
    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2020-11-20
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多