2019.01.07 更新:
fwrite 自 2016 年 11 月 25 日起加入 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")
根据speeding up the performance of write.table 下显示的详细基准测试,fwrite 比那里的write.csv (YMMV) 快约 17 倍。
2015 年 12 月 15 日更新:
将来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等。
--