[英]write.csv for large data.table
我有一個不是很大的data.table
(2 GB),但是由於某種原因, write.csv
需要花費很長時間將其寫出(我從未真正完成等待),並且似乎要使用大量的RAM來做它。
我試着轉換data.table
到data.frame
雖然這實在不應該做任何事情,因為data.table
延伸data.frame
。 有人碰到這個嗎?
更重要的是,如果您使用Ctrl - C停止它,R似乎不會退還內存。
更新2019.01.07 :
自2016-11-25以來, fwrite
一直在CRAN上。
install.packages("data.table")
更新08.04.2016 :
fwrite
最近已添加到data.table包的開發版本中。 它也並行運行(隱式)。
# Install development version of data.table
install.packages("data.table",
repos = "https://Rdatatable.github.io/data.table", type = "source")
# Load package
library(data.table)
# Load data
data(USArrests)
# Write CSV
fwrite(USArrests, "USArrests_fwrite.csv")
根據加速write.table性能下顯示的詳細基准測試, fwrite
比那里的write.csv
(YMMV)快17倍。
更新15.12.2015 :
將來data.table
包中可能會有fwrite
函數,請參見: https : //github.com/Rdatatable/data.table/issues/580 。 在該線程中,鏈接了一個GIST,它為此類功能提供了原型,將處理過程加快了2倍(根據作者, https://gist.github.com/oseiskar/15c4a3fd9b6ec5856c89 )。
原始答案 :
我遇到了同樣的問題(試圖編寫更大的CSV文件),最終決定不使用CSV文件。
我建議您使用SQLite,因為它比處理CSV文件要快得多:
require("RSQLite")
# Set up database
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname = "test.db")
# Load example data
data(USArrests)
# Write data "USArrests" in table "USArrests" in database "test.db"
dbWriteTable(con, "arrests", USArrests)
# Test if the data was correctly stored in the database, i.e.
# run an exemplary query on the newly created database
dbGetQuery(con, "SELECT * FROM arrests WHERE Murder > 10")
# row_names Murder Assault UrbanPop Rape
# 1 Alabama 13.2 236 58 21.2
# 2 Florida 15.4 335 80 31.9
# 3 Georgia 17.4 211 60 25.8
# 4 Illinois 10.4 249 83 24.0
# 5 Louisiana 15.4 249 66 22.2
# 6 Maryland 11.3 300 67 27.8
# 7 Michigan 12.1 255 74 35.1
# 8 Mississippi 16.1 259 44 17.1
# 9 Nevada 12.2 252 81 46.0
# 10 New Mexico 11.4 285 70 32.1
# 11 New York 11.1 254 86 26.1
# 12 North Carolina 13.0 337 45 16.1
# 13 South Carolina 14.4 279 48 22.5
# 14 Tennessee 13.2 188 59 26.9
# 15 Texas 12.7 201 80 25.5
# Close the connection to the database
dbDisconnect(con)
有關更多信息,請參見http://cran.r-project.org/web/packages/RSQLite/RSQLite.pdf
您還可以使用http://sqliteadmin.orbmu2k.de/之類的軟件來訪問數據庫並將數據庫導出為CSV等。
-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.