[R] Array analogue of row and col

Tony Plate tplate at acm.org
Sat Apr 6 19:21:21 CEST 2013


slice.index() in base

On 4/2/2013 9:53 AM, Enrico Bibbona wrote:
> Great!
> Thanks a lot, Enrico
>
>
> 2013/4/2 Duncan Murdoch <murdoch.duncan at gmail.com>
>
>> On 02/04/2013 6:36 AM, Enrico Bibbona wrote:
>>
>>> Is there any function that extends to multidimentional arrays the
>>> functionalities of "row" and "col" which are just defined for matrices?
>>> Thanks, Enrico Bibbona
>>>
>> Not as far as I know, but there are a lot of functions in packages.
>>
>> You could write your own something like this.  I've skipped any error
>> checking; you'll want to add that.
>>
>> indices <- function(a, which) {
>>    d <- dim(a)
>>    prod_before <- prod(d[seq_len(which-1)])
>>    result <- rep(seq_len(d[which]), each=prod_before,  length.out = prod(d))
>>    dim(result) <- d
>>    result
>> }
>>
>> Then indices(a, 1) gives an array of the same shape as a where entry i, j,
>> k, ... is i, indices(a, 2) has entry i,j,k, ... equal to j, etc.
>>
>> Duncan Murdoch
>>
>>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list