Re: [Rd] array extraction

From: robin hankin <hankin.robin_at_gmail.com>
Date: Wed, 28 Sep 2011 11:12:26 +1300

thank you Simon.

I find a[M] working to be unexpected, but consistent with (a close reading of) Extract.Rd

Can we reproduce a[,M]?

[I would expect this to extract a[,j,k] where M[j,k] is TRUE]

try this:

> a <- array(1:30,c(3,5,2))
> M <- matrix(1:10,5,2) %% 3==1
> a[M]

 [1] 1 4 7 10 11 14 17 20 21 24 27 30

This is not doing what I would want a[,M] to do.

I'll checkout afill() right now....

best wishes

Robin

On Wed, Sep 28, 2011 at 10:39 AM, Simon Knapp <sleepingwell_at_gmail.com> wrote:
> a[M] gives the same as your `cobbled together' code.
>
> On Wed, Sep 28, 2011 at 6:35 AM, robin hankin <hankin.robin@gmail.com>
> wrote:
>>
>> hello everyone.
>>
>> Look at the following R idiom:
>>
>>  a <- array(1:30,c(3,5,2))
>>  M <- (matrix(1:15,c(3,5)) %% 4) < 2
>>  a[M,] <- 0
>>
>> Now, I think that "a[M,]" has an unambiguous meaning (to a human).
>> However, the last line doesn't work as desired, but I expected it
>> to...and it recently took me an indecent amount of time to debug an
>> analogous case.  Just to be explicit, I would expect a[M,] to extract
>> a[i,j,] where M[i,j] is TRUE.  (Extract.Rd is perfectly clear here, and R
>> is
>> behaving as documented).
>>
>> The best I could cobble together was the following:
>>
>>  ind <- which(M,arr.ind=TRUE)
>>  n <- 3
>>  ind <-
>> cbind(kronecker(ind,rep(1,dim(a)[n])),rep(seq_len(dim(a)[n]),nrow(ind)))
>>  a[ind] <- 0
>>
>>
>> but the intent is hardly clear, certainly compared to "a[M,]"
>>
>> I've been pondering how to implement such indexing, and its
>> generalization.
>>
>> Suppose 'a' is a seven-dimensional array, and M1 a matrix and M2 a
>> three-dimensional array (both Boolean).  Then "a[,M1,,M2]" is a
>> natural generalization of the above.  I would want a[,M1,,M2] to
>> extract a[i1,i2,i3,i4,i5,i6,i7] where M1[i2,i3] and M[i5,i6,i7] are
>> TRUE.
>>
>> One would need all(dim(a)[2:3] == dim(M1)) and all(dim(a)[5:7] ==
>> dim(M2)) for consistency.
>>
>> Can any R-devel subscribers advise?
>>
>>
>>
>>
>> --
>> Robin Hankin
>> Uncertainty Analyst
>> hankin.robin_at_gmail.com
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
Robin Hankin
Uncertainty Analyst
hankin.robin_at_gmail.com

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 27 Sep 2011 - 22:22:38 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 28 Sep 2011 - 08:20:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive