Reputation: 873
I'm attempting to import and export, in pieces, a single 10GB CSV file with roughly 10 million observations. I want about 10 manageable RData files in the end (data_1.RData
, data_2.Rdata
, etc.), but I'm having trouble making the skip
and nrows
dynamic. My nrows
will never change as I need almost 1 million per dataset, but I'm thinking I'll need some equation for skip=
so that every loop it increases to catch the next 1 million rows. Also, having header=T
might mess up anything over ii=1
since only the first row will include variable names. The following is the bulk of the code I'm working with:
for (ii in 1:10){
data <- read.csv("myfolder/file.csv",
row.names=NULL, header=T, sep=",", stringsAsFactors=F,
skip=0, nrows=1000000)
outName <- paste("data",ii,sep="_")
save(data,file=file.path(outPath,paste(outName,".RData",sep="")))
}
Upvotes: 0
Views: 751
Reputation: 193677
(Untested but...) You can try something like this:
nrows <- 1000000
ind <- c(0, seq(from = nrows, length.out = 10, by = nrows) + 1)
header <- names(read.csv("myfolder/file.csv", header = TRUE, nrows = 1))
for (i in seq_along(ind)) {
data <- read.csv("myfolder/file.csv",
row.names = NULL, header = FALSE,
sep = ",", stringsAsFactors = FALSE,
skip = ind[i], nrows = 1000000)
names(data) <- header
outName <- paste("data", ii, sep = "_")
save(data, file = file.path(outPath, paste(outName, ".RData", sep = "")))
}
Upvotes: 1