[R] Subsetting an array by a vector of dimensions
Richard Pearson
richard.pearson at postgrad.manchester.ac.uk
Fri Jul 11 20:00:05 CEST 2008
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(expression(x), "[", subsetString, "]")
eval(parse(text=evalString))
}
"subsetArray<-"<- function(x, subset, value) {
subsetString <- paste(subset, collapse=",")
subsetString <- gsub("NA","",subsetString)
evalString <- paste(expression(x), "[", subsetString, "] <-", expression(value))
eval(parse(text=evalString))
x
}
x <- array(1:24, dim=2:4)
subsetArray(x, c(1,1,2))
subsetArray(x, c(1,1,2)) <- 25
x
Thanks to Pat!
Richard
Richard Pearson wrote:
> 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 assignment too. I need to read up more on writing assignment
> functions to do this. However, I'm thinking that someone has already
> solved this, in a far more elegant way than my hacking :-). Here's my
> function for anyone interested:
>
>> subsetArray <- function(x, subset) {
> + subsetString <- paste(subset, collapse=",")
> + subsetString <- gsub("NA","",subsetString)
> + evalString <- paste(expression(x), "[", subsetString, "]")
> + eval(parse(text=evalString))
> + }
>> x <- array(1:24, dim=2:4)
>> subsetArray(x, c(1,1,2))
> [1] 7
>> subsetArray(x, c(1,NA,2))
> [1] 7 9 11
>> subsetArray(x, c(1,NA,NA))
> [,1] [,2] [,3] [,4]
> [1,] 1 7 13 19
> [2,] 3 9 15 21
> [3,] 5 11 17 23
>>
>> subsetArray(x, c(1,1,2)) <- 25
> Error in subsetArray(x, c(1, 1, 2)) <- 25 : could not find function
> "subsetArray<-"
>
> Best wishes
>
> Richard.
>
>
> Patrick Burns wrote:
>> 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
>> patrick at 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")
>>
>> Richard Pearson wrote:
>>> 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:
>>>
>>>> x[1,1,]
>>> [1] 1 7 13 19
>>>> dims2<- c(1,1,NA)
>>>
>>> And have a function of x and dims2 that gives me back [1] 1 7 13 19
>>>
>>> Does such a thing exist?
>>>
>>> Thanks
>>>
>>> Richard
>>>
>>>
>>
>
--
Richard D. Pearson richard.pearson at postgrad.manchester.ac.uk
School of Computer Science, http://www.cs.man.ac.uk/~pearsonr
University of Manchester, Tel: +44 161 275 6178
Oxford Road, Mob: +44 7971 221181
Manchester M13 9PL, UK. Fax: +44 161 275 6204
More information about the R-help
mailing list