# [R] Multidimensional array

David Winsemius dwinsemius at comcast.net
Tue Dec 29 00:38:19 CET 2015

```> On Dec 28, 2015, at 1:10 PM, veni arakelian <veniarakelian at yahoo.com> wrote:
>
> Dear David
>
> The first three arrays are of the form
> val(:,:,1) =
>
>      1     1     1
>      1     1     1
>      1     1     1
>
>
> val(:,:,2) =
>
>      1     0     0
>      0     1     1
>      0     1     1
>
>
> val(:,:,3) =
>
>      1     0     0
>      0     1     1
>      0     1     1
>
> So I plan to import them in R as a single time series (i.e., 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 0 1 1 0 1 1) and 'reshape' it in the form val[ , , t].

I do not think you have demonstrated great powers of reading comprehension. I asked that you post the file as it would appear when viewed in a text editor. I also asked that you post in HTML, and it does not appear that you did that either. Please read the Posting Guide and do whatever self-study is needed so that you can configure your email client to post in plain text.

I'm going to make the assumption that the file looks like:

1     1     1
1     1     1
1     1     1
1     0     0
0     1     1
0     1     1
1     0     0
0     1     1
0     1     1

We can make a text/character representation of such a file for demonstration purposes:

txt <- "1     1     1
1     1     1
1     1     1
1     0     0
0     1     1
0     1     1
1     0     0
0     1     1
0     1     1"

The `scan` function can read from either character vectors or from files. The matrix function has a byrow argument whose default os FALSE but I think we need to set it to TRUE.

a <- matrix( scan(text=txt), ncol=3, byrow=TRUE)

So now we have a 9 x 3 matrix and need to stratify into 3 3x3 matrices within an array-classed object. Simply using `array( a, c(3,3,3) )` will not succeed. However, one can reshape that value into the desired object with `aperm`

aperm( array(a, c(3,3,3) ) , c(1,3,2) )

#   -- result --------
, , 1

[,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1

, , 2

[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    1
[3,]    0    1    1

, , 3

[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    1
[3,]    0    1    1

-- David.

>
> If there is another way more efficient, please let me know.
>
> Kind regards,
> Veni
>
> From: David Winsemius <dwinsemius at comcast.net>
> To: veni arakelian <veniarakelian at yahoo.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Monday, December 28, 2015 9:24 PM
> Subject: Re: [R] Multidimensional array
>
>
> > On Dec 27, 2015, at 11:49 PM, veni arakelian via R-help <r-help at r-project.org> wrote:
> >
> >
> >
> > Hi All.
> >
> > I'm very new in R. I'd like to import data of the form MxMxT, i.e., MxM array in T distinct points.
> > Any ideas how to do that?
>
> It will depend on the form in which this data is represented as a file object. Can you show the appearance of the first two or three MxM items as they would be viewed in a text editor. And PLEASE post in plain text.
>
>
> > Regards,
> > Veni
> >
> >
> >     [[alternative HTML version deleted]]
>
> Postin in HTML is generally a sign that you have not read the posting-guide page.
>
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > and provide commented, minimal, self-contained, reproducible code.
>
>
> David Winsemius
> Alameda, CA, USA
>
>
>

David Winsemius
Alameda, CA, USA

```