I use read.csv.sql from the read.csv.sql package to try and read in a subset of the rows where the subset selects from several values ββ- these values ββare stored in another vector.
I have hacked a way to a form that works, but I would like to see the correct way to pass the sql statement.
Below is an example below.
library(sqldf) # some data write.csv(mtcars, "mtcars.csv", quote = FALSE, row.names = FALSE) # values to select from variable 'carb' cc <- c(1, 2) # This only selects last value from 'cc' vector read.csv.sql("mtcars.csv", sql = paste("select * from file where carb = ", cc )) # So try using the 'in' operator - this works read.csv.sql("mtcars.csv", sql = "select * from file where carb in (1,2)" ) # but this doesn't read.csv.sql("mtcars.csv", sql = paste("select * from file where carb in ", cc )) # Finally this works read.csv.sql("mtcars.csv", sql = paste("select * from file where carb in ", paste("(", paste(cc, collapse=",") ,")")))
The last line above works, but is there an easier way to convey this statement, thanks.
source share