如何将数据从RpostgreSQL拼写到表结构中

时间:2015-09-08 11:36:30

标签: r

我正在运行以下脚本

library("knitr", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
library("markdown", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
library("ggplot2", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
library("RPostgreSQL", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
drv <- dbDriver("PostgreSQL")
conn <-dbConnect(drv,host=xxx,port='5439',dbname='stats',user=zzz,password=yyy)
dbListConnections(drv)
Data1 <-dbSendQuery(conn,"select family,date(ts),count(*),count(distinct uid)as Users from mr_session_log group by family,date(ts) order by date(ts)")
fetch(Data1,n=-1)

并接收我想要的结果,但是当我想用下面的代码将数据拼写到表结构时

Data1_df <- data.frame("Data1")

我正在尝试运行视图(Data1_df),我只收到一行,其中一列的值为Data1。

如何将获取后收到的数据(Data1,n = -1)拼写成表格结构(我想在该表格上运行一些图表)

附加了data1

                     family       date count users
1                     Other 2015-08-31  1000    12
2                     Other 2015-09-01  3156    74
3                     Other 2015-09-02  1258    12
4                     Other 2015-09-03  1116    31
5                     Other 2015-09-04   794    28
6                     Other 2015-09-05   390    14
7                     Other 2015-09-06   250    15
8         Samsung SM-G900FD 2015-09-07    72     1
9                     Other 2015-09-07   227     7
10                  LG D956 2015-09-07    47     1
11         Samsung GT-N7105 2015-09-07   245     1
12         Samsung SM-N910C 2015-09-07    24     1
13               Motorola   2015-09-07  3315    37
14         Samsung GT-I9505 2015-09-07   108     1
15         Samsung SM-N910F 2015-09-07   394     1

1 个答案:

答案 0 :(得分:2)

试试

data <- fetch(Data1,n=-1)

但你应该使用

Data1_df <- dbFetch(Data1,n=-1)

而不是fetch,因为fetch很快就会被弃用。

此外,如果您运行data.frame函数,则不必引用该对象,因此您宁愿使用

Data1_df <- data.frame(Data1)