简体   繁体   中英

Selecting NA values from sql file in R

I have sql tables being read in R with the following code

library(RSQLite)

setwd("C:/Users/Cat/Downloads")
drv = dbDriver("SQLite")
# Use the driver to connect to a particular sqlite database file
con = dbConnect(drv, "cartype")

dbListTables(con)

And there are columns named as ID and credit in a table Sale. Some of the credit is missing and I can select them with the following code.

wow = dbGetQuery(con, 'SELECT DISTINCT ID FROM Sale WHERE
                 credit IS NOT \"NA\";')

Question is how can I select ID with Date is NA? I tried the code

wow = dbGetQuery(con, 'SELECT DISTINCT ID FROM Sale WHERE
                 credit IS \"NA\";')

OR

wow = dbGetQuery(con, 'SELECT DISTINCT ID FROM Sale WHERE
                 credit == \"NA\";')

The codes work but giving a incorrect result that there's 0 result matching the condition, while it should have more than 100 IDs with NA credit.

Can anyone help me out, and show me how can I get IDs with NA credit? Thanks !

I think you're coming up against a mis-aprehension about how missings are stored in databases. These are usually stored as NULLs as opposed to NA.

Your first statement works because it is comparing a column of some type (eg date, int, varchar) against a string ("NA") and this will exclude NULLs because a string comparison (whether implicit or explicit) will always exclude missings and since all your dates will be different to "NA" it will return all non-missing records.

The reason why your second and third statements go onto return 0 records is because it is again doing a string comparison which will exclude NULLs and also won't find a match.

For SQLite, there is a great page on how NULLs are handled which might help you out with more detail on this topic: http://sqlite.org/nulls.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM