# Re: [Rd] x[1,], x[1,,], x[1,,,], ...

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Wed 23 Nov 2005 - 12:06:24 GMT

You can look at the definition of 'corner' in the public domain area of the Burns Statistics website. It uses 'do.call' on '[' to achieve (sort of) what you want.

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")

Henrik Bengtsson wrote:

>Hi,
>
>is there a function in R already doing what I try to do below:
>
># Let 'x' be an array with *any* number of dimensions (>=1).
>x <- array(1:24, dim=c(2,2,3,2))
>...
>x <- array(1:24, dim=c(4,3,2))
>
>i <- 2:3
>
>ndim <- length(dim(x))
>if (ndim == 1)
> y <- x[i]
>else if (ndim == 2)
> y <- x[i,]
>else if (ndim == 3)
> y <- x[i,,]
>else ...
>
>and so on. My current solution is
>
>ndim <- length(dim(x))
>args <- rep(",", ndim)
>args <- "i"
>args <- paste(args, collapse="")
>code <- paste("x[", args, "]", sep="")
>expr <- parse(text=code)
>y <- eval(expr)
>
>ndim <- length(dim(x))
>args <- rep(",", ndim)
>args <- "i"
>args <- paste(args, collapse="")
>code <- paste("x[", args, "]", sep="")
>expr <- parse(text=code)
>y <- eval(expr)
>
>Is there another way I can do this in R that I have overlooked?
>
>/Henrik
>
>______________________________________________
>R-devel@r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>
>
>

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Nov 23 23:51:28 2005

This archive was generated by hypermail 2.1.8 : Wed 23 Nov 2005 - 14:21:33 GMT