HI.
I make this code:
getdata<-function('a','b','c'
,'d','e','f'){
drv <- dbDriver("SQLite")
con<-dbConnect(drv, "sqlite.db")
lt<-dbListTables(con)
myf<-data.frame(NULL)
for (i in 1:length(lt))
{
myfile<-dbReadTable(con,lt[i])
myfile1<-myfile[-c(14:44)]
myfile1$MODEL<-gsub(" ", "", myfile1$MODEL)
library(RSQLite)
library(sqldf)
my_file<-sqldf("SELECT *
FROM myfile1 where MODEL in ('a','b','c'
,'d','e','f')")
my.file.spes<-conv(my_file)
myf<-rbind(myf,my.file.spes)
}
wd<-myf[order(myf$Product, myf$Time),]
wd1<-wd[!duplicated(wd),]
wd2<-as.data.frame(as.matrix(wd1),stringsAsFactors = FALSE)
wd3<-transform(wd2, Price = as.numeric(Price),
Volume = as.numeric(Volume))
dbDisconnect(con)
return(wd3)
}
I want this program also work when I use
wd3<-getdata(a,b,c,d,e,f,g,h...)
for any length of my selection. How should I do this then?
Thanks-
Tammy
[[alternative HTML version deleted]]
Jessica Streicher
2012-Nov-06 13:25 UTC
[R] how Can make function for selecting the products
If i understand this correctly, what you want to use is a vector or a list as a
parameter to that function.
getdata<-function(letterVec){
...
query<-paste("SELECT *FROM myfile1 where MODEL in",
sqlListFromVector(letterVec))
...
}
sqlListFromVector<-function(v){
if(is.numeric(v)){
help<-paste(v,sep="",collapse=",")
}
else{
help<-paste("'",v,"'",sep="",collapse=",")
}
result<-paste("(",help,")",sep="")
return(result)
}
Then you can put any character or number in letterVec, e.g.
letterVec<-c("a","b","c")
getdata(letterVec)
does that help?
On 06.11.2012, at 13:13, Tammy Ma wrote:
>
> HI.
>
> I make this code:
>
> getdata<-function('a','b','c'
,'d','e','f'){
>
> drv <- dbDriver("SQLite")
> con<-dbConnect(drv, "sqlite.db")
>
> lt<-dbListTables(con)
>
> myf<-data.frame(NULL)
>
> for (i in 1:length(lt))
>
> {
> myfile<-dbReadTable(con,lt[i])
> myfile1<-myfile[-c(14:44)]
> myfile1$MODEL<-gsub(" ", "", myfile1$MODEL)
> library(RSQLite)
> library(sqldf)
> my_file<-sqldf("SELECT *
> FROM myfile1 where MODEL in ('a','b','c'
,'d','e','f')")
> my.file.spes<-conv(my_file)
> myf<-rbind(myf,my.file.spes)
>
> }
> wd<-myf[order(myf$Product, myf$Time),]
> wd1<-wd[!duplicated(wd),]
> wd2<-as.data.frame(as.matrix(wd1),stringsAsFactors = FALSE)
> wd3<-transform(wd2, Price = as.numeric(Price),
> Volume = as.numeric(Volume))
>
> dbDisconnect(con)
>
> return(wd3)
>
> }
>
>
> I want this program also work when I use
>
> wd3<-getdata(a,b,c,d,e,f,g,h...)
>
> for any length of my selection. How should I do this then?
>
>
> Thanks-
>
>
> Tammy
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.