Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle column names not supported by sqldf in R

Tags:

r

sqldf

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?

like image 866
Prasun Velayudhan Avatar asked Dec 01 '25 08:12

Prasun Velayudhan


2 Answers

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' )
like image 96
Vincent Zoonekynd Avatar answered Dec 02 '25 22:12

Vincent Zoonekynd


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")
like image 20
Hazem HASAN Avatar answered Dec 02 '25 23:12

Hazem HASAN