I need to create a matrix which has 80000 rows and 80000 columns. But, after reading on Rbloggers, I got to know that, the number of elements in a matrix cannot exceed 2^31 - 1. My plan to avoid this problem for my particular algorithm is to use a data frame instead of a matrix. Is there a way I can create an empty data frame of dimension 80000 x 80000 without first creating a matrix and then converting it to a data.frame using as.data.frame like below?
myMatrix <- matrix(0, ncol = 40, nrow = 90)
myDataFrame <- as.data.frame(myMatrix)
You could construct an empty data frame of size 80,000 x 80,000 as follows:
dat <- do.call(data.frame, replicate(80000, rep(FALSE, 80000), simplify=FALSE))
dim(dat)
# [1] 80000 80000
dat[1,1]
# [1] FALSE
dat[80000,80000]
# [1] FALSE
Basically you build a list containing each column of the data frame you want to build (I built the list with replicate
with simplify=FALSE
) and then you build a data frame out of this with do.call
and the data.frame
function.
A few notes:
Though allocating a matrix of this size did not fail at allocation in 64-bit linux (R version 3.2.0), basic operations don't appear to work:
x <- matrix(0, nrow=80000, ncol=80000)
dim(x)
# [1] 80000 80000
x[1,1]
# Error: long vectors not supported yet: subset.c:733
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With