[R] preallocating matrices and rda read-back objects

From: Alexy Khrabrov <deliverable_at_gmail.com>
Date: Tue, 08 Apr 2008 23:00:55 -0700

I've read in Phil Spector's new book that it's a good idea to preallocate a big matrix, like

u <- matrix(0,nrow,ncol) # (1)

Now, I read contents of a huge matrix from a Fortran binary dump.

u <- readBin(con,what="double",n=nrow*ncol) # (2)

If I do (1) and then (2), u is a vector, obviously it's either reallocated or its matrix nature is lost -- overridden? overwritten?

Instead, I do it now as

u <-
matrix(readBin(con,what="double",n=nrow*ncol),nrow=nrow,ncol=ncol) # (3)

What's going on with memory management here and what's the right way to make it efficient -- and how to preallocate?

After that, I'm saving u as R binary object in an rda file. Does it make sense to preallocate u before reading it back now from the rda file?


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Apr 2008 - 06:05:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 10:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive