Вопрос

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!

Это было полезно?

Решение

The code has these problems:

  1. 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.

  2. 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.

  3. 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')")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top