Dom*_*bey -2 database sqlite r
我想访问和操作R中的大型数据集.因为它是一个大的CSV文件(~0.5 GB),我打算将它导入
SQLite然后从R访问它.我知道sqldf和RSQLite包可以做到这一点但我去了他们的手册并没有帮助.作为SQL的新手也无济于事.
我想知道我是否必须将R目录设置为SQLite,然后从那里开始?我如何读入R中的数据库呢?哎呀,如果你知道如何在不使用SQL的情况下从R访问数据库,请告诉我.
谢谢!
这真的很容易 - sqlite db文件的路径和文件名作为'database'参数传递.这是CRANberries做的:
databasefile <- "/home/edd/cranberries/cranberries.sqlite"
## ...
## main worker function
dailyUpdate <- function() {
stopifnot(all.equal(system("fping cran.r-project.org", intern=TRUE),
"cran.r-project.org is alive"))
setwd("/home/edd/cranberries")
dbcon <- dbConnect(dbDriver("SQLite"), dbname = databasefile)
repos <- dbGetQuery(dbcon,
paste("select max(id) as id, desc, url ",
"from repos where desc!='omegahat' group by desc")
# ...
Run Code Online (Sandbox Code Playgroud)
这就是真的.当然,稍后会有其他疑问......
在从R尝试或直接从R尝试之前,您可以轻松地在sqlite客户端中测试所有SQL查询.
编辑:由于上面显然太简洁了,这里有一个直接来自文档的例子:
con <- dbConnect(SQLite(), ":memory:") ## in-memory, replace with file
data(USArrests)
dbWriteTable(con, "arrests", USArrests)
res <- dbSendQuery(con, "SELECT * from arrests")
data <- fetch(res, n = 2)
data
dbClearResult(res)
dbGetQuery(con, "SELECT * from arrests limit 3")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6512 次 |
| 最近记录: |