我正在尝试使用sqldf,使用sqldf中的示例4在R数据框中插入新列

abbr <- data.frame (species = levels(iris[,"Species"]),
                    abbr = c("s","ve","vi"))
sqldf("select abbr, avg(Sepal_Length) from iris natural join abbr group by species")

sqldf("select abbr, avg(Sepal_Length) from iris join abbr using(Species) group by Species")


这两个sqldf命令都可以工作,但是当我将abbr的列名从abbr更改为abbr_col时,它失败了,我不知道应该更改sql语法中的哪个abbr

谢谢。

更新#01

> abbr <- data.frame (species = levels(iris[,"Species"]),
+                     abbr_col = c("s","ve","vi"))
> sqldf("select abbr_col, avg(Sepal_Length) from iris natural join abbr_col group by species")
Error in sqliteExecStatement(con, statement, bind.data) :
  RS-DBI driver: (error in statement: no such table: abbr_col)


我尝试将所有abbr重命名为abbr_col,但是失败。

最佳答案

一个“ abbr”是表名,另一个是列名:

abbr_table <- data.frame (species = levels(iris[,"Species"]),
                          abbr_col = c("s","ve","vi"))
sqldf("select abbr_col, avg(Sepal_Length) from iris natural join abbr_table group by species")

关于sqlite - 使用SQLite添加新列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7076447/

10-12 19:59