0

我迭代了一个日期数组,用于创建 sql 查询。

日期 <- c("2010-06-29", "2010-07-09", "2010-07-22")

fn1 <- function (N) 
{
    queryStart <- "SELECT something FROM somethingelse WHERE table.date ='"
    queryEnd <- "'AND table.something = 'foo'"
    for(i in 1:length(N)) {
        query1 <- paste(queryStart,N[i],queryEnd)
        result <- sqlQuery(con, query1)  //print(sqlQuery(con,query1) works as intended
    }
    return (result)
}

fn1(日期)

如何将所有内容放在一个数据框中?有人可以帮我吗?

4

1 回答 1

1

试试这个,虽然我没有测试过......

fn1 <- function (N) 
{
    queryStart <- "SELECT something FROM somethingelse WHERE table.date ='"
    queryEnd <- "'AND table.something = 'foo'"
    allQueries<-data.frame(matrix(nrow=0,ncol=0));
    for(i in 1:length(N)) {
        query1 <- paste(queryStart,N[i],queryEnd)
        result <- sqlQuery(con, query1)  //print(sqlQuery(con,query1) works as intended
        if (nrow(allQueries) == 0){
            allQueries<-result;
        } 
        else{
            allQueries<-rbind(allQueries,result);
        }
    }
    return (allQueries);
}
于 2013-02-21T10:15:08.933 回答