[R] how convert an array to a matrix?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Apr 13 15:24:35 CEST 2000


Sven Garbade <garbade at psy.uni-muenchen.de> writes:

> Dear R-list,
> 
> is it possible to convert a threedimensional array to a matrix? The
> array has the form
> 
> , , -4
> 
>       -4 -3   -2 -1    0  1    2  3    4
> -80 1588 NA 1171 NA 1121 NA  852 NA 1580
> 0   1497 NA 1311 NA  762 NA 1185 NA 1466
> 80  1960 NA 1257 NA  941 NA 1435 NA 1636
> 
> [...]
> 
> I need a matrix like
> 
> -80 -4 -4 1588
> -80 -3 -4 NA
> -80 -2 -4 1171
> etc.
> 
> 
> I´ve tried write.table() with no succes:


The way to do it would be something like

cbind(as.vector(slice(x,1)),
	as.vector(slice(x,2)),
	as.vector(slice(x,3)),
	as.vector(x))

where slice is the generalised row()/col()/... function which we
haven't got in R (yet):

slice<-function(x,i) sweep(x, i, 1:dim(x)[i], function(x,y) y )

Hmm...that's not quite right. You want the dimnames in there instead
of 1:dim(x)[i], but you get the general idea.
 
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list