Are you a good R citizen and preallocates your matrices? If you are allocating a numeric matrix in one of the following two ways, then you are doing it the wrong way!
x <- matrix(nrow=500, ncol=100)<br />or
x <- matrix(NA, nrow=500, ncol=100)<br />
Why? Because it is counter productive. And why is that? In the above, ‘x’ becomes a logical matrix, and not a numeric matrix as intended. This is because the default value of the ‘data’ argument of matrix() is
NA, which is a logical value, i.e.
> x <- matrix(nrow = 500, ncol = 100)<br />> mode(x)<br /> "logical"<br />> str(x)<br /> logi [1:500, 1:100] NA NA NA NA NA NA ...<br />Why is that bad? Because, as soon as you assign a numeric value to any of the cells in ‘x’, the matrix will first have to be coerced to numeric when a new value is assigned. The originally allocated logical matrix was allocated in vain and just adds an unnecessary memory footprint and extra work for the garbage collector.
Instead allocate it using
x <- matrix(NA_real_, nrow=500, ncol=100)<br />Of course, if you wish to allocate a matrix with all zeros, use
The exact same thing happens with array(), which is also because the default value is
> x <- array(dim=c(500,100))<br />> mode(x)<br /> "logical"<br />Similarly, be careful when you setup vectors using rep(), e.g. compare
x <- rep(NA, times=500)<br />to
x <- rep(NA_real_, times=500)<br />Note, if all you want is an empty vector with all zeros, you may as well use
x <- double(500)<br />for doubles and
x <- integer(500)<br />for integers.
In the ‘base’ package there is a neat little function called tracemem() that can be used to trace the internal copying of objects. We can use it to show how the two cases differ. Lets start by doing it the wrong way:
> x <- matrix(nrow=500, ncol=100)<br />> tracemem(x)<br /> "<0x00000000100a0040>"<br />> x[1,1] <- 3.14<br />tracemem[0x00000000100a0040 -> 0x000007ffffba0010]:<br />> x[1,2] <- 2.71<br />><br />That ‘tracemem’ output message basically tells us that ‘x’ is copied, or more precisely that a new internal object (0x000007ffffba0010) is allocated and that ‘x’ now refers to that instead of the original one (0x00000000100a0040). This happens because ‘x’ needs to be coerced from logical to numerical before assigning cell (1,1) the (numerical) value 3.14. Note that there is no need for R to create a copy in the second assignment to ‘x’, because at this point it is already of a numeric type (and there are no other variables referring to it).
To avoid the extra copy, lets make sure to allocate a numeric matrix from the start and there will be no extra copies created:
> x <- matrix(NA_real_, nrow=500, ncol=100)<br />> tracemem(x)<br /> "<0x000007ffffd70010>"<br />> x[1,1] <- 3.14<br />> x[1,2] <- 2.71<br />><br />
R version 3.1.0 Patched (2014-06-11 r65921)<br />Platform: x86_64-w64-mingw32/x64 (64-bit)<br /><br />locale:<br /> LC_COLLATE=English_United States.1252 <br /> LC_CTYPE=English_United States.1252 <br /> LC_MONETARY=English_United States.1252<br /> LC_NUMERIC=C <br /> LC_TIME=English_United States.1252 <br /><br />attached base packages:<br /> stats graphics grDevices utils datasets methods base <br /><br />other attached packages:<br />  markdown_0.7 R.cache_0.10.0 knitr_1.6 ggplot2_1.0.0 <br />  R.devices_2.9.2 lineprof_0.1 pryr_0.1 devtools_1.5 <br />  R.utils_1.32.5 R.oo_1.18.2 R.methodsS3_1.6.2<br /><br />loaded via a namespace (and not attached):<br />  base64enc_0.1-1 codetools_0.2-8 colorspace_1.2-4 digest_0.6.4 <br />  evaluate_0.5.5 formatR_0.10 grid_3.1.0 gtable_0.1.2 <br />  httr_0.3 MASS_7.3-33 memoise_0.2.1 mime_0.1.1 <br /> munsell_0.4.2 parallel_3.1.0 plyr_1.8.1 proto_0.3-10 <br /> R.rsp_0.19.0 Rcpp_0.11.2 RCurl_1.95-4.1 reshape2_1.4 <br /> scales_0.2.4 stringr_0.6.2 tools_3.1.0 whisker_0.3-2 <br />