2016-07-29 137 views
1

我需要知道爲什麼會出現提到錯誤,儘管當我用任何數值代替DFinal [i,「x」]時工作正常,感謝你的幫助sqliteSendQuery(con,statement,bind.data)中的錯誤:error in statement:[

for(i in 1:nrow(DFinal)){ 
+  result =0 
+  var1= DFinal[i,"x"] 
+  var2= DFinal[i,"y"] 
+  result <- sqldf(' select count(distinct(V5)) from DLoc where V1= DFinal[i,"x"] and V5 in (select distinct(V5) from DLoc where V1=var2) ') 
+  
+  DFinal[i,"res"]<- result 
+  
+ } 

錯誤sqliteSendQuery(CON,聲明,bind.data): 錯誤的語句:近 「[我,」 X 「]」:語法錯誤

回答

0

您查詢沒有任何意義,因爲您指的是R變量,而是使用變量'的值

for (i in 1:nrow(DFinal)) { 
    result <- 0 
    var1 <- DFinal[i, "x"] 
    var2 <- DFinal[i, "y"] 
    query <- paste0('select count(distinct V5) from DLoc where V1 = ', var1, 
        ' and V5 in (select distinct V5 from DLoc where V1 = ', var2, ')') 
    result <- sqldf(query) 

    DFinal[i, "res"] <- result 
} 

您的查詢看起來可能還需要一些改進,但上面的代碼應該至少允許您運行它。

另一種評論:通常不建議將原始SQL查詢連接在一起,因爲它會打開SQL注入攻擊的可能性。但是,當你在一個可能是封閉的R環境中運行這個查詢時,它可能是正常的。