R SQLite在使用COUNT时将char转换为数字

时间:2015-11-21 14:42:11

标签: r sqlite

我通过R总结一个SQLite数据库表,但是当我使用COUNT()时,它将我的字符字段转换为数字,即如果我有观察Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.text.JTextComponent.getSelectionStart(Unknown Source) at javax.swing.text.PlainView.paint(Unknown Source) at javax.swing.text.FieldView.paint(Unknown Source) at javax.swing.plaf.basic.BasicTextUI$RootView.paint(Unknown Source) at javax.swing.plaf.basic.BasicTextUI.paintSafely(Unknown Source) at javax.swing.plaf.basic.BasicTextUI.paint(Unknown Source) at javax.swing.plaf.basic.BasicTextUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$1200(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.plaf.basic.BasicComboBoxUI.isPopupVisible(Unknown Source) at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.getXPComboBoxState(Unknown Source) at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.paintXPComboBoxBackground(Unknown Source) at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$1200(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.DefaultListCellRenderer.getListCellRendererComponent(Unknown Source) at javax.swing.plaf.basic.BasicComboBoxUI.getDisplaySize(Unknown Source) at javax.swing.plaf.basic.BasicComboBoxUI.getMinimumSize(Unknown Source) at com.sun.java.swing.plaf.windows.WindowsComboBoxUI.getMinimumSize(Unknown Source) at javax.swing.JComponent.getMinimumSize(Unknown Source) at java.awt.GridLayout.minimumLayoutSize(Unknown Source) at java.awt.Container.minimumSize(Unknown Source) at java.awt.Container.getMinimumSize(Unknown Source) at javax.swing.JComponent.getMinimumSize(Unknown Source) at javax.swing.BoxLayout.checkRequests(Unknown Source) at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at javax.swing.RepaintManager$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 我得到{{ 1}}回来。我已经在SQLite Manager for Firefox中尝试了这个查询,它按预期工作;所以我希望问题与RSQLite有关。

他是问题的一个有效例子:

0, 1, 2, n

我想知道:

  1. 为什么要这样做?
  2. 如何解决?
  3. 我使用的是:R 3.2.2,RStudio 0.99.489,Ubuntu 14.04,RSQLite 1.0.0& DBI 0.3.1

2 个答案:

答案 0 :(得分:4)

发生这种情况的原因是TXT列声明为TEXT类型,而不是内置类型。如果它已被声明为类型TXT,它将按预期工作。

在SQLite中,列的类型只是一个提示,该列的每一行仍然可以具有不同的类型。由于txt不是已知类型,实际上发生了什么。在下面的示例中,txt2TXT列在CREATE语句中声明为TXT,而TEXT不是SQLite中的内置类型({ {1}}是)。正如我们在y列下面的typeof(txt)列中看到的那样,显示txt列的每一行的类型确实针对不同的行具有不同的类型。同样显示txt2列中显示的typeof(txt2)列。当这样的数据被读回R时,第一个条目被R用作类型,因此具有ijnteger类型的第一个txt条目的0被读入R作为整数并且txt2具有带有文本类型的n的第一个条目作为字符读入R中。通过查看下面的str(y)输出可以看出这一点。我们还注意到text列作为字符读入R,因为text是SQLite中的已知数据类型。

library(RSQLite)
db <- dbConnect(SQLite(), ":memory:")

dbSendQuery(db, "CREATE TABLE d (txt TXT, txt2 TXT, text TEXT)")
x <- data.frame(txt = c(0, "n"), txt2 = c("n", 0), text2 = c(0, "n")) 
dbWriteTable(db, "d", x, append = TRUE, row.names = FALSE)

y <- dbGetQuery(db, 
         "select txt, typeof(txt), txt2, typeof(txt2), text, typeof(text) from d")

y
##  txt typeof(txt) txt2 typeof(txt2) text typeof(text)
## 1   0     integer    n         text    0         text
## 2   0        text    0      integer    n         text

str(y)
## 'data.frame':   2 obs. of  6 variables:
## $ txt         : int  0 0   <------ becomes int since 1st entry of txt is int
## $ typeof(txt) : chr  "integer" "text"
## $ txt2        : chr  "n" "0"  <---- chr since 1st entry is text
## $ typeof(txt2): chr  "text" "integer"
## $ text        : chr  "0" "n"  <--- chr since column declared as text in create
## $ typeof(text): chr  "text" "text"

有关详细信息,请参阅sqlite documentation on datatypes

答案 1 :(得分:1)

问题在于您的CREATE TABLE命令。它应该是TEXT而不是TXT

dbSendQuery(db, "
   CREATE TABLE data
      (Year INT,
      Station TEXT,
      Obs TEXT)
")