[R] arrayInd and which
Keith S Weintraub
kw1958 at gmail.com
Fri Apr 5 16:08:30 CEST 2013
David,
Thanks for your help and time. Your explanation was just what I needed.
And to the rest of you good R folks I would still be interested to see a working example of arrayInd, what it's supposed to do and what it's used for.
I have read the doc page a number of times and it's still not clear to me. There is no example of the use of the function on that page either.
At this point it's not an emergency I am just curious.
Thanks again,
KW
--
On Apr 3, 2013, at 2:04 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Apr 3, 2013, at 10:59 AM, David Winsemius wrote:
>
>>
>> On Apr 3, 2013, at 7:53 AM, Keith S Weintraub wrote:
>>
>>> Folks,
>>>
>>> I have Googled but not found much regarding arrayInd aside from the "which" help page.
>>>
>>> Any good examples or docs on what arrayInd does that is better or different from which()?
>>>
>>> In addition take the following 20x10 matrix:
>>>
>>> td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6,
>>> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6,
>>> 6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6,
>>> 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6,
>>> 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2,
>>> 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6,
>>> 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6,
>>> 4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6,
>>> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6,
>>> 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L
>>> ))
>>>
>>> I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column.
>>>
>>> Is there a better way to do this other than:
>>>
>>> bounds<-c(rep(5,5), rep(4,5))
>>> idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE)
>>>
>>>> idxs
>>> row col
>>> [1,] 1 1
>>> [2,] 13 1
>>> [3,] 13 2
>>> [4,] 1 3
>>> [5,] 8 3
>>> [6,] 13 3
>>> [7,] 1 4
>>> [8,] 13 4
>>> [9,] 1 5
>>> [10,] 13 5
>>> [11,] 1 6
>>> [12,] 4 6
>>> [13,] 13 6
>>> [14,] 4 7
>>> [15,] 13 7
>>> [16,] 1 8
>>> [17,] 4 8
>>> [18,] 13 8
>>> [19,] 3 9
>>> [20,] 1 10
>>> [21,] 13 10
>>>
>>> Lastly can you explain these results:
>>>
>>>> td[idxs[10,]]
>>> [1] 4 6
>>>
>>>> td[idxs[10,1]]
>>> [1] 4
>>>
>>>> td[idxs[10,2]]
>>> [1] 6
>>>
>>>> td[idxs[10,3]]
>>> Error: subscript out of bounds
>>
>> This has nothing to do with the behavior of arrayInd and everything to do with the behavior of "[".
>>
>>> td[idxs[10,drop=FALSE] ]
>> [1] 4
>
> Arrgh. The explanation was correct, but there is a missing comma. Should be:
>
> td[idxs[10, ,drop=FALSE] ]
>
> Only shows up if you chose a different row
>
>> td [ idxs[12, drop=FALSE] ]
> [1] 6 WRONG
>
>> td [ idxs[12, , drop=FALSE] ]
> [1] 1 Correct
>
> --
> David.
>
>> When extracting from a matrix with a result of asingle row the extracted object looses its matrix attributes and becomes a numeric vector. That behavior is prevented with drop=FALSE and desire results accrue.
>>
>> This would not have been a puzzle if you had chose multiple rows at a time:
>>
>>> td [ idxs[1:2, ] ]
>> [1] 1 4
>>> td [ idxs ]
>> [1] 1 4 1 1 3 3 1 5 3 4 2 1 1 5 3 2 2 4 1 2 3 3
>>
>>
>
> David Winsemius
> Alameda, CA, USA
>
More information about the R-help
mailing list