0

I have a large number (17000) of 96x97 matrices with names b1,b2...b17000. I need to combine them into an array 96x97x17000. I am trying to do this through an array function:

s=array(b1,b2...b17000,dim=c(96,97,17000))

The problem is that for this function to work, you need to write down the name of all the matrices. How can this be done without writing down the name of the matrices 17,000 times?

I tried to set this as a range b1:b17000 but it does not work correctly.

Sergey D.
  • 3
  • 1
  • You may want to read my answer at [How to make a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207) for some helpful context about this. – Gregor Thomas Jun 03 '20 at 13:48

1 Answers1

1

Based on your original question the below code should work for you.

names<-c(paste0("b",1:17000))
s<-array(unlist(mget(names)),dim=c(96,97,17000))

This will create a vector called names containing your the names of your matrices (assuming they're actually called m1, m2,... m17000). For example: names[1] will be b1 and names[2] will be b2, so on and so forth.

You can then use names to reference your matrices array as shown in my suggested code.

I hope this helps!

Joshua Mire
  • 730
  • 1
  • 4
  • 17
  • Thanks for your feedback. The code you proposed creates a vector with the names of the matrices, but when you create an array, it makes an array of names. And I need the array to be from the values contained in the matrices. – Sergey D. Jun 03 '20 at 13:11
  • Sorry about that! I omitted the `get()` function. I've updated the answer to reflect this. – Joshua Mire Jun 03 '20 at 13:14
  • 1
    Maybe change your `get(names)` to `unlist(mget(names))`. – GKi Jun 03 '20 at 13:28
  • Thank you so much! unlist (mget (names)) is what I needed. – Sergey D. Jun 03 '20 at 13:50