Search code examples
rrmysql

Inserting character vector elements in a single character value


I have a column of names in my dataframe in R and I am trying to create a text variable of an SQL query out of the names as follows...

select * from *table* where *name* in (*data$name[1,]*, *data$name[2,]*...)

While this can be done this using a for loop and paste function, but I am hoping to find an alternative to using a loop. Any idea on how to go about this ?


Solution

  • This should work:

    x <- paste0("select * from table where name in (", paste(names(data), collapse=", "), ")")