我正在考虑使用RPostgresQL
包将数据库中的数据直接导入r。到目前为止,我使用Postico
(一个PostgreSQL客户端)软件编写查询,并将其导出为csv,然后将csv文件导入R。
这是我到目前为止写的,不知道下一步该怎么做。
library('RPostgreSQL')
pg=dbDriver("PostgreSQL")
con = dbConnect(pg, user="msahil515", password="",
host="localhost", port=5432, dbname="msahil515")
在此之后,我如何将数据库中的表加载到R中,或者如何在R中编写查询,以便只从数据库中提取必要的数据?
发布于 2019-02-15 11:03:18
这是你问题的直接答案。这肯定可以扩展。
library('RPostgreSQL')
#create connection object
con <- dbConnect(drv =PostgreSQL(),
user="msahil515",
password="",
host="localhost",
port=5432,
dbname="msahil515")
dbListTables(con) #list all the tables
#query the database and store the data in datafame
first_results <- dbGetQuery(con, "SELECT * from FOO limit 10;")
dbDisconnect(con) #disconnect from database
https://stackoverflow.com/questions/53177653
复制相似问题