Build a dynamic size array in R

I was wondering what are the ways to build a dynamic size array in R.

For one example, I want to build an n-vector, but its dimension n is determined dynamically. The following code will work:

> x=NULL > n=2; > for (i in 1:n) x[i]=i; > x [1] 1 2 

In another example, I want to build a matrix n by 2, where the number of rows n is dynamically determined. But I can’t even assign the first line:

 > tmp=c(1,2) > x=NULL > x[1,]=tmp Error in x[1, ] = tmp : incorrect number of subscripts on matrix > x[1,:]=tmp Error: unexpected ':' in "x[1,:" 

Thank you and welcome!

+4
source share
6 answers

You can measure the array after it is filled (in one-dimensional, vector order)
Emulation of a 1-dimensional fragment of the question, here, how can this be done with higher dimensions.

 > x=c() > tmp=c(1,2) > n=6 > for (i in seq(1, by=2, length=n)) x[i:(i+1)] =tmp; > dim(x) = c(2,n) > x [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 > 

Instead of using i:(i+1) as an index, it may be preferable to use seq(i, length=2) or even better, seq(i, length=length(tmp)) for a more general approach, as shown below (for example with 4 x 7 array)

 > x=c() > tmp=c(1,2,3,4) > n=7 > for (i in seq(1, by=length(tmp), length=n)) x[seq(i, length=length(tmp))] = tmp; > dim(x) = c(length(tmp),n) > x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 2 [3,] 3 3 3 3 3 3 3 [4,] 4 4 4 4 4 4 4 > 

We can also get a similar result by rearranging x with cbind / rbind, as follows.

 > tmp=c(1,2) > n=6 > x=rbind(tmp) > for (i in 1:n) x=rbind(x, tmp); > x [,1] [,2] tmp 1 2 tmp 1 2 tmp 1 2 tmp 1 2 tmp 1 2 tmp 1 2 tmp 1 2 

Note: you can get rid of the names "tmp" (this is a side effect of rbind) using > dimnames(x)=NULL

+4
source

I think the answers you are looking for are rbind () and cbind ():

 > x=NULL # could also use x <- c() > rbind(x, c(1,2)) [,1] [,2] [1,] 1 2 > x <- rbind(x, c(1,2)) > x <- rbind(x, c(1,2)) # now extend row-wise > x [,1] [,2] [1,] 1 2 [2,] 1 2 > x <- cbind(x, c(1,2)) # or column-wise > x [,1] [,2] [,3] [1,] 1 2 1 [2,] 1 2 2 

The strategy of trying to assign "new indexes" on the go, as you tried, can be done in some languages, but it can't be done that way in R.

You can also use sparse matrices provided in the Matrix package. They would allow assignments of the form M <- sparseMatrix(i=200, j=50, x=234) , which would lead to a single value in row 200, column 50 and 0 everywhere.

  require(Matrix) M <- sparseMatrix(i=200, j=50, x=234) M[1,1] # [1] 0 M[200, 50] # [1] 234 

But I think that the use of sparse matrices is best used for subsequent use after mastering regular matrices.

+6
source

You can enable it:

 tmp = c(1,2) x = NULL rbind(x, tmp) 
+2
source

I believe this is the approach you need

 arr <- array(1) arr <- append(arr,3) arr[1] <- 2 print(arr[1]) 

(located on rosettacode.org)

+2
source

When I want to dynamically build an array (matrix), I do it like this:

 n <- 500 new.mtrx <- matrix(ncol = 2, nrow = n) head(new.mtrx) [,1] [,2] [1,] NA NA [2,] NA NA [3,] NA NA [4,] NA NA [5,] NA NA [6,] NA NA 

Now your matrix is ​​ready to accept vectors.

Assuming you already have a vector, you pass it to the matrix() function. Notice how the values ​​are "broken down" into a matrix (column-wise). This can be changed using the byrow argument.

  matrix(letters, ncol = 2) [,1] [,2] [1,] "a" "n" [2,] "b" "o" [3,] "c" "p" [4,] "d" "q" [5,] "e" "r" [6,] "f" "s" [7,] "g" "t" [8,] "h" "u" [9,] "i" "v" [10,] "j" "w" [11,] "k" "x" [12,] "l" "y" [13,] "m" "z" 
+1
source
 n = 5 x = c(1,2) %o% rep(1,n) x # [,1] [,2] [,3] [,4] [,5] # [1,] 1 1 1 1 1 # [2,] 2 2 2 2 2 x = rep(1,n) %o% c(1,2) x # [,1] [,2] # [1,] 1 2 # [2,] 1 2 # [3,] 1 2 # [4,] 1 2 # [5,] 1 2 
0
source

All Articles