The 'corner' function uses 'do.call' on '[' to perform a
similar task. The code is available in the Public Domain
Code area of the Burns Statistics website.
The reason for 'corner' being there was not to answer
questions like this, but because it is useful when working
with (largish) matrices and higher dimensional arrays. It
shows you a small corner of the array so you can see that
it looks like what you expect, or to look at old objects to
see what are in them.
Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
Agin.Patrick@hydro.qc.ca wrote:
Dear S-Users,
I'm trying to extract from an array x of arbitrary dimension in the
following way :
y <- x[,1] #if x is length(dim(x)==2)
y <- x[,,1] #if x is length(dim(x)==3)
y <- x[,,,1] #if x is length(dim(x)==4)
etc..
The only solution I found is to transform x as a vector, extract the
first n elements and rearrange the result as an array of appropriate
dimensions:
v <- as.vector(x)
lv <- length(v)
d <- dim(x)
ld <- length(dim(x))
n <- lv/d[ld]
y <- array(v[1:n],dim=d[1:(ld-1)])
Is there a better way to do that?
Regards,
Patrick
|