How to handle column names not supported by sqldf in R

喜你入骨 提交于 2020-01-15 04:57:11

问题


I've a data frame where some of the column names are of the format . format. For ex: Company.1 when i'm using that column in a sqldf function it throws an error

data=sqldf(select Company.1 from test)
Error in sqliteExecStatement(con, statement, bind.data) : 
RS-DBI driver: (error in statement: near ".1": syntax error)

Any workaround so that i can use the column name as it is?


回答1:


The dot has another meaning in SQL (e.g., separating table name from column name) and is replaced by an underscore before sending the data to SQLite.

library(sqldf)
test <- data.frame( "Company.1" = 1:10 )
sqldf( 'SELECT Company_1 FROM test' )



回答2:


This problem is about the . in your column name, if you change it to Company_1 it works:

data = sqldf("select Company_1 from test")



回答3:


The solution for the latest update of sqldf is answered here

We only need to write the SQL statement between single quotes, and the column names including dots between double quotes or backticks/backquotes interchangeably.



来源:https://stackoverflow.com/questions/19019883/how-to-handle-column-names-not-supported-by-sqldf-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!