Byron
Byron

Reputation: 43

how to construct an array by row in R?

I am trying to construct an array using

> byrow=TRUE

inside of

> array()

But this function is not available.

For example: I type in

y<-array(1:24,c(4,3,2))
y

I get

, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

But I want the numbers to be arranged by row. So I tried

y<-array(1:24,c(4,3,2),byrow=TRUE)

But I got

Error in array(1:24, c(4, 3, 2), byrow = TRUE) : unused argument (byrow = TRUE)

How can I achieve what I want?

Also, how to arrange the numbers by the other dimensions?

Upvotes: 1

Views: 542

Answers (1)

user7396508
user7396508

Reputation:

You could use aperm to transpose the array...

aperm(array(1:24,c(3,4,2)), c(2,1,3))

Upvotes: 6

Related Questions