Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "Large Matrix" and regular numeric matrix?

Tags:

r

matrix

rstudio

When a relatively large matrix is created, Rstudio marks it as a Large Matrix in its environment window:

 x <- matrix(rnorm(10000 * 5000), ncol=5000)
 # Large matrix (50000000 elements, 381.5 Mb)

The mode() function as expected returns "numeric" for this object:

mode(x)
## [1] "numeric"

If however I run the following command:

mode(x) <- "numeric"

Rstudio changes "Large Matrix" into a regular numeric matrix:

# x:  num [1:10000, 1:5000]

So what is the difference between these 2 objects? Does this difference exist in Rstudio only or these two objects are different in R as well?

like image 552
Katia Avatar asked Nov 26 '19 03:11

Katia


People also ask

What is the limit to matrices in R?

You can have more than 2^31-1 elements in a vector. Matrices are vectors with dim attributes. You can have more than 2^31-1 elements in a matrix (ie n times k ) Your row and column index are still limited to 2^31.

How do you define a matrix in R?

To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order.

What does as matrix do in R?

as. matrix converts its first argument into a matrix, the dimensions of which will be inferred from the input. matrix creates a matrix from the given set of values. as.


1 Answers

In my understanding, "Large Matrix" and matrix is the same matrix object. What matters is how these objects are displayed in the global environment in RStudio.

RStudio also distinguishes between vectors and large vectors. Consider the following vector:

n <- 256
v1 <- rnorm(n*n-5)

This vector is listed as a large vector. Now, let's decrease its size by one:

v2 <- rnorm(n*n-6)

Suddenly, it becomes a normal vector. The structure of both objects is the same (which can be verified by running str). So is their class and storage mode. What is different then? Notice that the size of v2 in memory is exactly 512 Kb.

lobstr::obj_size(v2)
>524,288 B # or exactly 512 kB

The size of v1 is slightly greater:

lobstr::obj_size(v1)
>524,296 B # or 512.0078125 KB

As far as I understand (correct me if I am wrong), for mere convenience RStudio displays objects that are greater than 512 kB differently.

like image 146
slava-kohut Avatar answered Oct 18 '22 03:10

slava-kohut