[R] Read vector as multi-dimensional data in R by row

arun smartpink111 at yahoo.com
Wed Jul 11 01:36:57 CEST 2012


Hi HJ,

No problem.

By changing the order of numbers in both perm and dim, you can create different combinations
A.K.



----- Original Message -----
From: "yhj204 at googlemail.com" <yhj204 at googlemail.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Tuesday, July 10, 2012 7:25 PM
Subject: Re: [R] Read vector as multi-dimensional data in R by row

Dear arun,

That code works. Thanks so much for the hints!

Best wishes,
HJ


Sent using BlackBerry® from Orange

-----Original Message-----
From: arun <smartpink111 at yahoo.com>
Date: Mon, 9 Jul 2012 22:12:03 
To: HJ YAN<yhj204 at googlemail.com>
Reply-To: arun <smartpink111 at yahoo.com>
Cc: R help<r-help at r-project.org>
Subject: Re: [R] Read vector as multi-dimensional data in R by row

Hi,

Try this:
b1<-aperm(array(a,dim=c(5,2,2)),perm=c(2,1,3))
> b1
, , 1

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

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]   11   12   13   14   15
[2,]   16   17   18   19   20

A.K.



----- Original Message -----
From: HJ YAN <yhj204 at googlemail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, July 9, 2012 7:25 PM
Subject: [R] Read vector as multi-dimensional data in R by row

Dear R users


Say I wanted to read a vector into R as multi-dimensional array by row,
e.g.

a<-c(1:20)

> b<-array(a,dim=c(2,5,2))
> b
, , 1

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

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]   11   13   15   17   19
[2,]   12   14   16   18   20


But actually I wanted...

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

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]   11   12   13   14   15
[2,]   16   17   18   19   20


I checked '?array' but there is not an argument or something  like
'byrow=T' as the one in 'matrix'.

Could anyone help please?

Many thanks in advance!

HJ

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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