[Rd] array extraction
(Ted Harding)
ted.harding at wlandres.net
Wed Sep 28 01:28:41 CEST 2011
Somewhat out of my depth here (I have only 2 arms, but am
swimming in waters which require 3): My interpretation is
that a and M are basically vectors, with dimension attributes,
accessed down the columns.
The array 'a' consists of 30 elements 1:30 in that order,
accessed by each of 3 rows for each of 5 columns in each
of two "layers", in that order of precedence.
The matrix M consusts of 15 elements, accessed by each
of 35 rows for each of 5 columns.
Thus a(M) treats M as an selection vector, ignoring dimension,
and reads along a and at the same time along M, selecting
according to TRUE of FALSE. Then, when it gets to the end
of the first "layer" in 'a' it re-cycles M.
When I tried a[M,] I got
a[M,]
# Error in a[M, ] : incorrect number of dimensions
I infer that it is treating the M as a vector, so there
are only 2 dimensions in a[M,] instead of 3. So try:
a[M,,]
# Error: (subscript) logical subscript too long
which is not surprising since one is applying a 15-long
selector to the first dimension of 'a', which has only
length 3, just as in
a[rep(TRUE,15),,]
# Error: (subscript) logical subscript too long
which, interestingly, differs from
a[rep(1,15),,]
# (Output, which is what you'd expect, omitted)
(Hmm, out of my depth again ... ).
Well, maybe two arms is not enough when you need three
to swim in these waters; but it seems that one long swishy
tail will do nicely. That being said, I still find the
water quite murky!
Ted.
On 27-Sep-11 22:12:26, robin hankin wrote:
> 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 at 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
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 28-Sep-11 Time: 00:28:37
------------------------------ XFMail ------------------------------
More information about the R-devel
mailing list