Exporting Table from R to PostgreSQL by RPostgreSQL

不问归期 提交于 2019-12-12 09:53:01

问题


I am using RPostgreSQL I have done number of process and I got a table in R. I wanted to put this table in PostgreSQL from R for further analysis that I will use PostgreSQL codes. The problem is that when I have a table in R which doesn't exist in PostgreSQL, I cannot perform SQL Codes on it.

Sample Table s_2 the data format is not data frame and temp is float and DateeTIMEE is timestamps:

       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

I have tries following code:

library(sqldf)
sqldf("select * from s_2 where temp > -1 ")

but I came up with this error:

 Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect postgres@localhost on dbname "test" ) Error in !dbPreExists : invalid argument type.

Moreover, could I use table s_2 in this syntax while it is a temporary variable in R and doesn't exist in db?


回答1:


Table called "p25" can easily exported to the DB from the following code:

dbWriteTable(con, "p25",p25,overwrite = T )



回答2:


The question is a little be confusing , but you can use sqldf package to perform sql queries

library(sqldf)
sqldf("select * from s_2 where temp > -1 ")

e.g : I just put your s_2 in a 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


来源:https://stackoverflow.com/questions/14193034/exporting-table-from-r-to-postgresql-by-rpostgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!