Reputation: 11
I'm working with the sqldf package in R. I want to create a dataset whose students' ID are excluded from another dataset. My code looks like:
delete <- sqldf("select distinct ID from A where ...")
B<-sqldf("select * from A where ID not in ('select ID from delete')")
After running the code I found B is exactly the same as A. It seems sqldf doesn't recogonize the nested select statement. Any suggestions would be appreciated!
Upvotes: 0
Views: 2332
Reputation: 269421
The code has these problems:
there is actually no nested select at all in the code shown. The statement is requesting all ID's that do not equal the string 'select ID from delete'
. Remove the quotes.
delete
is an SQLite keyword. Either use a different name for your data.frame or put the name delete
in quotes within the SQL statement so it knows not to treat it as a keyword.
next time please state the question in a reproducible form. See How to make a great R reproducible example?
Making these three changes we have the following where A has the ID's 1,2,3,4, delete has the IDs 1,2 and B has the ID's 3,4.
library(sqldf)
A <- data.frame(ID = c(1, 1, 2, 3, 4))
delete <- sqldf("select distinct ID from A where ID < 3")
B <- sqldf("select * from A where ID not in (select ID from 'delete')")
Upvotes: 3