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

From: Henrik Bengtsson <hb_at_maths.lth.se>
Date: Thu 24 Nov 2005 - 00:33:35 GMT

Hi, thanks everyone.

Some comments below:

Peter Dalgaard wrote:

> Henrik Bengtsson <hb@maths.lth.se> writes:
> 
> 

>>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[1] <- "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?
> 
> 
> I think this should work:
> 
> x <- array(1:24, dim=c(3,2,2,2)) # not c(2,2,3,2)....
> i <- 2:3
> ndim <- length(dim(x))
> ix <- as.list(rep(TRUE, ndim))
> ix[[1]] <- i
> do.call("[", c(list(x), ix))

In my case, 'x' is huge, an I have to be careful with allocating memory. Doesn't the 'list(x)' statement enforce an extra copy of 'x'? Or will lazy evaluation be able to pull out 'x' from the list again without evaluating 'list(x)'? I don't think so, but I'm not sure. There is also some overhead in 'ix[[1]] <- i', but 'i' is typically much smaller than 'x' so this should be of minor importance.

What about Andy's suggestion

   array(x[slice.index(x, 1) == 1], dim(x)[-1])?

There 'slice.index(x, 1)' will create an array of same size as 'x'.

I do not think the 'eval(parse(...))' has such overhead (correct me if I'm wrong), but on the other hand, it is a more "ugly" solution. I prefer not to use parse(), substitute() and friends in my code, if I don't have to.

I just want to bring up this flavor of the problem too, because I often find myself having to choose from similar options in other situations. If you have further comments, I would appreciate those.

Thanks

Henrik



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Nov 24 11:38:33 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:34 GMT