1

I am trying to insert a new column in a R dataframe using sqldf, using the example 4 from sqldf

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")

Both sqldf command works, but it fails when I change the column name of abbr from abbr to abbr_col, I don't know which abbr in the sql syntax should be changed.

Thanks.

update #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)

I tried renaming all abbr into abbr_col, but fail.

2
  • It's not clear what you want to do. Commented Aug 16, 2011 at 10:05
  • I modified the question with additional background info, thanks Commented Aug 16, 2011 at 10:07

2 Answers 2

2

One “abbr” is table name, another one is column name:

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")
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to rename column from abbr to abbr_col, then you need change all of the abbr occurrences to abbr_col.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.