我在R中使用sqlf来合并(加入)两个数据集。但是,我收到一条错误消息:
Error in .verify.JDBC.result(s, "Unable to execute JDBC statement ", statement) :
以下是一个例子:
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
library(sqldf)
library(tcltk)
df3 <- sqldf("SELECT CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID)")
运行后,我收到以下错误消息:
Error in .verify.JDBC.result(s, "Unable to execute JDBC statement ", statement) :
Unable to execute JDBC statement SELECT CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID) (Ambiguous column name "CustomerId"; SQL statement:
SELECT CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID) [90059-175])
这是sessionInfo()之后的输出:
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] grid tcltk stats graphics grDevices utils datasets methods base
other attached packages:
[1] sqldf_0.4-10 RSQLite_1.0.0 gsubfn_0.6-6 proto_0.3-10 Hmisc_3.16-0 ggplot2_1.0.1 Formula_1.2-1 survival_2.38-1 lattice_0.20-31 RH2_0.2.3 RJDBC_0.2-5 rJava_0.9-6
[13] DBI_0.3.1 chron_2.3-45
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 cluster_2.0.1 magrittr_1.5 splines_3.2.0 MASS_7.3-40 munsell_0.4.2 colorspace_1.2-6 stringr_1.0.0 plyr_1.8.2
[10] tools_3.2.0 nnet_7.3-9 gtable_0.1.2 latticeExtra_0.6-26 digest_0.6.8 gridExtra_0.9.1 RColorBrewer_1.1-2 reshape2_1.4.1 acepack_1.3-3.3
[19] rpart_4.1-9 stringi_0.4-1 scales_0.2.4 foreign_0.8-63
答案 0 :(得分:0)
你可能在运行问题中的代码之前加载了RH2 R包,因此sqldf使用H2数据库而不是SQLite。
如果您打算使用H2,请注意它不支持USING
关键字。有关H2语法,请参阅http://www.h2database.com。
如果您不打算使用H2,那么(i)不加载它,否则(ii)如果您需要加载它,那么通过drv = "SQLite"
显式指定sqlite数据库作为其中一个参数sqldf
或其他(iii)指定适当的全局选项:options(sqldf.driver = "SQLite")
。
另请注意,不需要library(tcltk)
行。
答案 1 :(得分:0)
错误消息不明确的列名&#34; CustomerId&#34; 表示数据库服务器有两个或多个包含CustomerId
列的表,并且它没有知道用哪个来满足select
- 条款。您需要明确指定要使用的表:
SELECT df1.CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID)
虽然对于仅出现在一个表中的列不是必需的,但最好对每个列执行此操作。