[R] avoiding eval parse with indexing

Berend Hasselman bhh at xs4all.nl
Sun May 26 16:41:28 CEST 2013


On 26-05-2013, at 15:56, Martin Ivanov <tramni at abv.bg> wrote:

> Hello,
> I would like to get an advice on how the notorious eval(parse()) construct could possibly 
> be avoided in the following example. I have an array x, which can have different number of dimensions,
> but I am only interested in extracting, say, the first element of the first dimension. Currently I achieve this
> in this way:
> 
> eval(parse(text=paste0("x[1", paste(rep(", ", length(dim(x)) - 1), collapse=""), "]")))
> 
> Is it possible to avoid the eval parse here? How?


I tried this

x1 <- array(runif(9),dim=c(3,3))
x2 <- array(runif(8),dim=c(2,2,2))

and then

x1[1] and x2[1] gave me what you wanted.
I don't know if it is the coRRect way to do what you want.

Berend



More information about the R-help mailing list