简体   繁体   English

从R中的sql文件中选择NA值

[英]Selecting NA values from sql file in R

I have sql tables being read in R with the following code 我在R中使用以下代码读取sql表

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. 并且表Sale中有名为ID和credit的列。 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? 问题是如何选择日期为NA的ID? 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. 该代码可以工作,但给出的结果不正确,即有0个结果与条件匹配,而它应具有100个以上不带信用的ID。

Can anyone help me out, and show me how can I get IDs with NA credit? 谁能帮助我,并告诉我如何获得具有NA信用额的ID? 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. 这些通常存储为NULL,而不是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. 您的第一条语句之所以有效,是因为它正在将某种类型的列(例如date,int,varchar)与字符串(“ NA”)进行比较,并且这将排除NULL,因为字符串比较(无论是隐式还是显式)将始终排除缺失,并且您的所有日期都将不同于“ NA”,它将返回所有不丢失的记录。

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. 您的第二个和第三个语句返回0记录的原因是因为它再次进行了字符串比较,该比较将排除NULL并且也找不到匹配项。

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 对于SQLite,有一个很棒的页面介绍了如何处理NULL,这可能会帮助您更详细地了解此主题: http : //sqlite.org/nulls.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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