可以在我的数据框上再使用sqldf(sql Exec语句中的错误)

时间:2014-05-23 23:11:26

标签: r sqldf

(我对R来说很新。)

我不知道为什么会收到以下错误:

> apps.rsd.c <- sqldf("SELECT appid FROM apps.rsd WHERE rcount > 50")
Error in sqliteExecStatement(con, statement, bind.data) : 
  RS-DBI driver: (error in statement: no such table: apps.rsd)

我认为这些可能有所帮助,我试图将它们与数据框进行比较,这些数据框让我可以使用sqldf,但是没有看到导致错误的原因:

> dput(head(apps.rsd))
structure(list(appid = c(173L, 717L, 996L, 209L, 602L, 255L), 
  cid = c(4L, 15L, 21L, 5L, 13L, 6L), price = c(0, 0, 0, 1.99, 
  0, 0.76), count = c(411, 411, 210, 18, 921, 22), sum = c(1226, 
  1870, 871, 66, 3948, 86), mean = c(2.98296836982968, 4.54987834549878, 
  4.14761904761905, 3.66666666666667, 4.28664495114007, 3.90909090909091
  ), sd = c(1.73897694746568, 0.958668345866094, 1.31370760232218, 
  1.7950549357115, 1.33373734360862, 1.62114131819336), rcount = c(3, 
  3, 3, 5, 5, 7), rsum = c(7, 0, 0, 13, 0, 19), rsd = c(2.3094010767585, 
  2.3094010767585, 2.3094010767585, 2.19089023002066, 2.19089023002066, 
  2.1380899352994)), .Names = c("appid", "cid", "price", "count", 
"sum", "mean", "sd", "rcount", "rsum", "rsd"), class = c("data.table", 
"data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x7fd3bc807b78>)

> str(apps.rsd)
Classes ‘data.table’ and 'data.frame':  1518 obs. of  10 variables:
  $ appid : int  173 717 996 209 602 255 1473 442 672 772 ...
  $ cid   : int  4 15 21 5 13 6 31 10 14 17 ...
  $ price : num  0 0 0 1.99 0 0.76 0 7.98 0 0.75 ...
  $ count : num  411 411 210 18 921 22 113 54 564 33 ...
  $ sum   : num  1226 1870 871 66 3948 ...
  $ mean  : num  2.98 4.55 4.15 3.67 4.29 ...
  $ sd    : num  1.739 0.959 1.314 1.795 1.334 ...
  $ rcount: num  3 3 3 5 5 7 7 2 2 2 ...
  $ rsum  : num  7 0 0 13 0 19 0 5 0 0 ...
  $ rsd   : num  2.31 2.31 2.31 2.19 2.19 ...
  - attr(*, ".internal.selfref")=<externalptr> 

1 个答案:

答案 0 :(得分:2)

Dot是SQL中的运算符,而不是名称中的合法字符,因此将apps.rsd放在单引号中(或将其重命名为某个没有点的名称):

sqldf("SELECT appid FROM 'apps.rsd' WHERE rcount > 50")

这是一个可重复的例子。请注意,我删除了数据框末尾的奇怪外部指针。 (那是怎么到达那里的?)

apps.rsd <- 
  structure(list(appid = c(173L, 717L, 996L, 209L, 602L, 255L), 
  cid = c(4L, 15L, 21L, 5L, 13L, 6L), price = c(0, 0, 0, 1.99, 
  0, 0.76), count = c(411, 411, 210, 18, 921, 22), sum = c(1226, 
  1870, 871, 66, 3948, 86), mean = c(2.98296836982968, 4.54987834549878, 
  4.14761904761905, 3.66666666666667, 4.28664495114007, 3.90909090909091
  ), sd = c(1.73897694746568, 0.958668345866094, 1.31370760232218, 
  1.7950549357115, 1.33373734360862, 1.62114131819336), rcount = c(3, 
  3, 3, 5, 5, 7), rsum = c(7, 0, 0, 13, 0, 19), rsd = c(2.3094010767585, 
  2.3094010767585, 2.3094010767585, 2.19089023002066, 2.19089023002066, 
  2.1380899352994)), .Names = c("appid", "cid", "price", "count", 
"sum", "mean", "sd", "rcount", "rsum", "rsd"), class = c("data.table", 
"data.frame"), row.names = c(NA, -6L))  

library(sqldf)
sqldf("SELECT appid FROM 'apps.rsd' WHERE rcount > 50")

输出结果为:

[1] appid
<0 rows> (or 0-length row.names)

或重命名:

apps_rsd <- apps.rsd
sqldf("SELECT appid FROM apps_rsd WHERE rcount > 50")

给出相同的输出。