[R] cbind alternate
Rui Barradas
ruipbarradas at sapo.pt
Fri Jan 6 21:16:54 CET 2012
Sorry Mary,
My function would write the remainder twice, I had only tested it
with multiples of the chunk size.
(And without looking at the lenghty output correctly.)
Now checked:
write.big.matrix <- function(x, y, outfile, nmax=1000){
if(file.exists(outfile)) unlink(outfile)
testf <- file(outfile, "at") # or "wt" - "write text"
on.exit(close(testf))
step <- nmax # how many at a time
inx <- seq(1, length(x)-step, by=step) # index into 'x' and 'y'
mat <- matrix(0, nrow=step, ncol=2) # create a work matrix
# do it 'nmax' rows per iteration
for(i in inx){
mat <- cbind(x[i:(i+step-1)], y[i:(i+step-1)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE,
col.names=FALSE)
}
# and now the remainder
if(i+step < length(x)){
mat <- NULL
mat <- cbind(x[(i+step):length(x)], y[(i+step):length(y)])
write.table(mat, file=testf, quote=FALSE, row.names=FALSE,
col.names=FALSE)
}
# return the output filename
outfile
}
x <- 1:(1e6 + 1234) # a numeric vector
y <- sample(letters, 1e6 + 1234, replace=TRUE) # and a character vector
length(x);length(y) # of the same length
fl <- "test.txt" # output file
system.time(write.big.matrix(x, y, outfile=fl, nmax=100))
user system elapsed
3.04 0.06 3.09
system.time(write.big.matrix(x, y, outfile=fl))
user system elapsed
1.64 0.12 1.76
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/cbind-alternate-tp4270188p4270687.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list