備忘録!!

面白いと思った記事や本を紹介したいと思います。

Rで空の行列を作る際の小ネタ(備忘)

昨日、Rを使ったデータ分析の授業で習った小ネタ(備忘メモ)

 

先生が「自分も最近までしらなかったんだけどね」との前置きで以下の記事を紹介してくれた 。

”PITFALL: Did you really mean to use matrix(nrow, ncol)?”

https://www.r-bloggers.com/pitfall-did-you-really-mean-to-use-matrixnrow-ncol/

”Best way to allocate matrix in R, NULL vs NA?”

https://stackoverflow.com/a/26724451

 

Rで空の行列を用意する際に、要素がすべてNAのものを作るのではなく、NA_real_ または NA_integer_ を用いるべきとのこと。

NAはlogicalなので、NA行列に数字を代入する際にはlogical -> numeric というcoercionが必要となる。

これはメモリの無駄遣いだよね、という話。 

 

正直現状のレベルではほぼ全く影響しないレベルの小ネタながら、Rの仕組みを理解する上では面白かった。

 

以下R-bloggersより引用。

 

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)

or

x <- matrix(NA, nrow=500, ncol=100)

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 logicalvalue, i.e.> x <- matrix(nrow = 500, ncol = 100)
> mode(x)
[1] "logical"
> str(x)
 logi [1:500, 1:100] NA NA NA NA NA NA ...

 

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 NA_real_ (or NA_integer_ for integers):x <- matrix(NA_real_, nrow=500, ncol=100)Of course, if you wish to allocate a matrix with all zeros, use 0 instead of NA_real_ (or 0L for integers).