[R] Initializing vector and matrices

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Thu Feb 29 15:31:36 CET 2024


You could declare a matrix much larger than you intend to use. This works with a few megabytes of data. It is not very efficient, so scaling up may become a problem.
m22 <- matrix(NA, 1:600000, ncol=6)

It does not work to add a new column to the matrix, as in you get an error if you try m22[ , 7] but convert to data frame and add a column

m23 <- data.frame(m22)
m23$x7 <- 12

The only penalty that I know of to having unused space in a matrix is the amount of memory it takes. One side effect is that your program may have a mistake that you would normally catch with a subscript out of bounds error but with the extra space it now runs without errors.

Tim



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Richard O'Keefe
Sent: Thursday, February 29, 2024 5:29 AM
To: Steven Yen <styen using ntu.edu.tw>
Cc: R-help Mailing List <r-help using r-project.org>
Subject: Re: [R] Initializing vector and matrices

[External Email]

x <- numeric(0)
for (...) {
    x[length(x)+1] <- ...
}
works.
You can build a matrix by building a vector one element at a time this way, and then reshaping it at the end.  That only works if you don't need it to be a matrix at all times.
Another approach is to build a list of rows.  It's not a matrix, but a list of rows can be a *ragged* matrix with rows of varying length.

On Wed, 28 Feb 2024 at 21:57, Steven Yen <styen using ntu.edu.tw> wrote:
>
> Is there as way to initialize a vector (matrix) with an unknown length
> (dimension)? NULL does not seem to work. The lines below work with a
> vector of length 4 and a matrix of 4 x 4. What if I do not know
> initially the length/dimension of the vector/matrix?
>
> All I want is to add up (accumulate)  the vector and matrix as I go
> through the loop.
>
> Or, are there other ways to accumulate such vectors and matrices?
>
>  > x<-rep(0,4)  # this works but I like to leave the length open  >
> for (i in 1:3){
> +  x1<-1:4
> +  x<-x+x1
> + }
>  > x
> [1]  3  6  9 12
>
>  > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to
> leave the dimension open
>       [,1] [,2] [,3] [,4]
> [1,]    0    0    0    0
> [2,]    0    0    0    0
> [3,]    0    0    0    0
> [4,]    0    0    0    0
>  > for (i in 1:3){
> +   y1<-matrix(17:32, nrow = 4, ncol = 4)
> +   y<-y+y1
> + }
>  > y
>       [,1] [,2] [,3] [,4]
> [1,]   51   63   75   87
> [2,]   54   66   78   90
> [3,]   57   69   81   93
> [4,]   60   72   84   96
>  >
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu
> %7Cdbccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638447993707432549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=
> PtWjcDOnwO7PArVOSdgYbpz8ksjDPK%2Bn9ySyhwQC0gE%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu%7Cdb
> ccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638447993707438911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Igb16
> CBYgG21HLEDH4I4gfjjFBa3KjDFK8yEZUmBo8s%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list