我正在使用 RPostgreSQL 我已完成了多个进程,我在R中得到了一个表。我想把这个表放在R的PostgreSQL中进行进一步分析,我将使用PostgreSQL代码。问题是当我在R中有一个在PostgreSQL中不存在的表时,我无法在其上执行SQL代码。
示例表s_2数据格式不是数据帧,temp是float,而DateeTIMEE是时间戳:
temp DateeTIMEE
1 -1.64 2007-09-29 00:01:09
2 -1.76 2007-09-29 00:03:09
3 -1.83 2007-09-29 00:05:09
4 -1.86 2007-09-29 00:07:09
5 -1.94 2007-09-29 00:09:09
6 -1.87 2007-09-29 00:11:09
7 -1.87 2007-09-29 00:13:09
8 -1.80 2007-09-29 00:15:09
9 -1.64 2007-09-29 00:17:09
10 -1.60 2007-09-29 00:19:09
11 -1.90 2007-09-29 00:21:09
12 -2.08 2007-09-29 00:23:09
13 -1.94 2007-09-29 00:25:09
14 -2.12 2007-09-29 00:27:09
15 -1.87 2007-09-29 00:29:09
16 -2.18 2007-09-29 00:31:09
17 -1.98 2007-09-29 00:33:09
18 -1.73 2007-09-29 00:35:09
19 -1.84 2007-09-29 00:37:09
20 -2.04 2007-09-29 00:39:09
21 -1.86 2007-09-29 00:41:09
22 -1.94 2007-09-29 00:43:09
23 -1.77 2007-09-29 00:45:09
24 -1.78 2007-09-29 00:47:09
25 -1.50 2007-09-29 00:49:09
26 -1.46 2007-09-29 00:51:09
27 -1.72 2007-09-29 00:53:09
28 -1.67 2007-09-29 00:55:09
29 -1.56 2007-09-29 00:57:09
30 -1.69 2007-09-29 00:59:09
31 -1.97 2007-09-29 01:01:09
32 -1.79 2007-09-29 01:03:09
33 -1.79 2007-09-29 01:05:09
34 -1.84 2007-09-29 01:07:09
35 -1.91 2007-09-29 01:09:09
36 -1.87 2007-09-29 01:11:09
37 -1.98 2007-09-29 01:13:09
38 -1.83 2007-09-29 01:15:09
39 -1.88 2007-09-29 01:17:09
40 -1.88 2007-09-29 01:19:09
我尝试了以下代码:
library(sqldf)
sqldf("select * from s_2 where temp > -1 ")
但我想出了这个错误:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect postgres@localhost on dbname "test" ) Error in !dbPreExists : invalid argument type.
此外,我是否可以在此语法中使用表s_2,而它是R中的临时变量而在db中不存在?
答案 0 :(得分:3)
名为“p25”的表可以通过以下代码轻松导出到DB:
dbWriteTable(con, "p25",p25,overwrite = T )
答案 1 :(得分:0)
问题有点令人困惑,但您可以使用sqldf
包来执行SQL查询
library(sqldf)
sqldf("select * from s_2 where temp > -1 ")
例如:我只是将你的s_2放在data.frame
> sqldf("select * from s_2 where temp < -2 ")
temp Datee TIMEE
1 -2.08 2007-09-29 00:23:09
2 -2.12 2007-09-29 00:27:09
3 -2.18 2007-09-29 00:31:09
4 -2.04 2007-09-29 00:39:09