-1

I am trying the following code :

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

here, med the function argument of the function

med_aff1<- function(med)

error coming:

Error in result_create(conn@ptr, statement) : unrecognized token: "'%"

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

0

You could simply paste to concatenate the strings.

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

Addendum:

I'm not sure what your function is supposed to do... but the output stored in the med variable should be a string valid as a part of an SQL statement.

Addendum 2:

If you want to loop through multiple elements you will need to figure out what to do with the different outputs of the different queries. As you did not state a clear direction, I've stored them in a list.

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)
}
Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • Thank you it works fine.But again another problem is the there if you can help me. 'med' is a list/array having {A,B}. When I am using the above query it is taking showing the rhs rules of {A,B} not {B,A}. What should I do ? – Koushik Roy Jul 11 '18 at 08:00
  • @KoushikRoy I've just updated the answer – Prometheus Jul 11 '18 at 08:18
  • Thank you.But , I think this will result in matches of medicines of "A" and medicine of "B" clubbed together. But I want the matches of the set{A,B} and set{B,A}. I am sending this 'med' as an API..suppose. med=A,B Now, the output I am having is all the possible outcomes of {A},{B},{A,B},{B,A}.. I need only rhs of {A,B} AND {B,A} – Koushik Roy Jul 11 '18 at 10:01