[R] Chopping a two column data frame by rows into a three dimensional array.

arun smartpink111 at yahoo.com
Tue Oct 16 02:34:25 CEST 2012


HI,
May be this:
dat <- cbind(x=c(1:100),y=c(100:1)) 

newarray<-array(do.call(cbind,lapply(split(dat,rep(1:10,each=10)),function(x) cbind(x=x[1:10],y=x[11:20]))),dim=c(10,2,10))
colnames(newarray)<-c("x","y")


 newarray[,,1]
  #     x   y
 #[1,]  1 100
 #[2,]  2  99
 #[3,]  3  98
 #[4,]  4  97
 #[5,]  5  96
 #[6,]  6  95
 #[7,]  7  94
 #[8,]  8  93
 #[9,]  9  92
#[10,] 10  91
A.K.



----- Original Message -----
From: Hans Thompson <hans.thompson1 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, October 15, 2012 7:58 PM
Subject: [R] Chopping a two column data frame by rows into a three dimensional array.

If I have a two column data frame like:

> dat <- cbind("x"=c(1:100),"y"=c(100:1))

How can I create an array that splits every ten rows of that data frame
into a third dimension of an array so that:

> newarray[,,1]

,,1

x   y
1   100
2   99
3   98
...   ...
10  91


,,2

x    y
11  90
12  89
...   ...


...

Thanks.

    [[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