Re: [R] Subsetting an array by a vector of dimensions

2008-07-24 Thread Kenn Konstabel
Returning to the old question ... > Is it possible to subset an n-dimensional array by a > vector of n dimensions? On Sat, Jul 12, 2008 at 1:34 AM, Wolfgang Huber <[EMAIL PROTECTED]> wrote: > > do.call("[", list(x,1,2,TRUE)) > [1] 3 9 15 21 > > See also http

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Wolfgang Huber
Kenn Konstabel wrote: What is "wrong" is, I think, that you can't write a substitute for x[1,2,] this way. (Or, in general, empty indexes won't work.) A related question - Is there a way to replace x[1,2,] with "["(1,2, *something clever*) ? Kenn Dear Kenn, I can: > do.call("[", list(x

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Kenn Konstabel
What is "wrong" is, I think, that you can't write a substitute for x[1,2,] this way. (Or, in general, empty indexes won't work.) A related question - Is there a way to replace x[1,2,] with "["(1,2, *something clever*) ? Kenn On Sat, Jul 12, 2008 at 12:48 AM, Wolfgang Huber <[EMAIL PROTECTED]>

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Wolfgang Huber
Hi Richard, what is wrong with Patrick's suggestion? I get x <- array(1:24, dim=2:4) x[rbind(c(1,1,2))] ## [1] 7 x[rbind(c(1,1,2))] <- 13 x[rbind(c(1,1,2))] ## [1] 13 And you could also do do.call("[", list(x,1,1,2)) These should be a bit quicker than the eval/parse constructs - see also

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Richard Pearson
In case anyone's still interested, I now have (I think!) a complete solution (thanks to a quick look at my new favourite document - S Poetry :-) subsetArray <- function(x, subset) { subsetString <- paste(subset, collapse=",") subsetString <- gsub("NA","",subsetString) evalString <- paste(e

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Richard Pearson
My understanding of matrix subscripting is that this can be used to access arbitrary elements from an array and return them as a vector, but I don't understand how that helps me here. I've now written a function that seems to do what I originally wanted, but I've also realised I want to do assi

Re: [R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Patrick Burns
I think you are looking for subscripting with a matrix: x[cbind(1,1,2)] See, for instance, the subscripting section of chapter 1 of S Poetry. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Richard Pearson

[R] Subsetting an array by a vector of dimensions

2008-07-11 Thread Richard Pearson
Hi Is it possible to subset an n-dimensional array by a vector of n dimensions? E.g. assume I have x <- array(1:24, dim=2:4) x[1,1,2] [1] 7 dims <- c(1,1,2) I would like a function that I can supply x and dims as parameters to, and have it return 7. Also, I would like to do something like