[R] extract positions from matrix

Bert Gunter gunter.berton at gene.com
Sun Nov 20 01:20:49 CET 2011


Inline below. -- Bert

On Sat, Nov 19, 2011 at 1:06 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Nov 19, 2011, at 1:15 PM, Bert Gunter wrote:
>
>> Folks:
>>
>> David: I believe your approach needs to be modified to accommodate
>> hundreds of matrices per the OP's specification.
>
> I suggested that my approach was most applicable if the questioner had his
> matrices already in a list. (He didn't say, so neither of us knows yet what
> his/her use case is.)
>
>>
>> However, I would say that this is exactly the situation in which
>> arrays should be used. Not only does it appear simpler, but it also
>> could be faster since once the array is created, (vectorized) indexing
>> is used. The key is to take advantage of column major ordering of
>> arrays in R as follows:
>>
>> 1. The OP's example specification is wrong -- he wants positions 1 and
>> 5 of the first COLUMN of each matrix (which both solutions gave, but
>> just didn't mention).
>>
>> 2. The code below creates the array assuming that the global workspace
>> has only the matrices in it. Subscript ls() appropriately if this is
>> not the case.
>>
>>> for(i in 1:3) assign(LETTERS[i], 15*(i-1)+seq_len(15)) ## toy data
>>> rm(i)  ## want only the matrices in the global workspace
>>
>>> ar <-  array( sapply(ls(), get), dim=c(5,3,3))  ## the dim argument could
>>> also be specified programatically
>>
>
> I wonder if you are doing ".jpg" a favor by putting ls() at the center of
> your strategy unless you offer the tools to isolate these objects from the
> rest of the global environment. Who works with no other objects in the
> workspace?
>

I think this is a good point. The real issue is how to get the
"hundreds of matrices" into a friendly data structure. If they were
already in a list, for example, then it would be even simpler to
convert them into an array, which I still think is the right data
structure for this sort of extraction.

Cheers,
Bert

> Perhaps this would add the extra level of encapsulation needed for the
> sapply(ls() method of access the created matrices in a probably less
> disruptive fashion:
>
> e <- new.env()
> with(e, { for(i in 1:3) assign( LETTERS[i], 15*(i-1)+seq_len(15) )
> rm(i)
> ar <-  array( sapply(ls(), get), dim=c(5,3,3)) })
>
> # ----------------
> e$ar[c(1,5),1, ]
>
>     [,1] [,2] [,3]
> [1,]    1   16   31
> [2,]    5   20   35
>
> --
> David.
>
>
>
>>> ar[c(1,5),1,]
>>
>>    [,1] [,2] [,3]
>> [1,]    1   16   31
>> [2,]    5   20   35
>>
>>
>> Cheers,
>> Bert
>>
>> On Sat, Nov 19, 2011 at 9:19 AM, David Winsemius <dwinsemius at comcast.net>
>> wrote:
>>>
>>> On Nov 19, 2011, at 9:32 AM, R. Michael Weylandt wrote:
>>>
>>>> Here's one approach:
>>>>
>>>> A=matrix(1:15,5)
>>>> B=matrix(15:29,5)
>>>> C=matrix(30:44,5)
>>>>
>>>> do.call(cbind, lapply(c("A","B","C"),function(x) get(x)[c(1,5),1]))
>>>
>>> Also:
>>>
>>> sapply( list(A,B,C), function(x) do.call("[", list(x, c(1,5)))  )
>>>
>>> Notice that this actually was extracting using what might be called the
>>> "vector positions". If you wanted to use the i,j version of "[" then you
>>> would need an extra column (this example pulling the second columns in
>>> the
>>> rows selected:
>>>
>>>> sapply(list(A,B,C),function(x) do.call("[", list(x, c(1,5), 2)) )
>>>
>>>    [,1] [,2] [,3]
>>> [1,]    6   20   35
>>> [2,]   10   24   39
>>>
>>>
>>> Comments on the differences: Michaels version used cbind to get the ruslt
>>> in
>>> a matrix, whereas mine used sapply. His used the get function to extract
>>> the
>>> objects from a character vector, whereas mine never constructed a
>>> character
>>> vector. Which one you deploy will depend on your data setup. If you
>>> already
>>> have these in a list, mine might be easier, but if they constitute an
>>> easily
>>> constructed set of names you might use LETTERS[] numbers and paste() to
>>> build your list of object names.
>>>
>>> --
>>> david.
>>>
>>>
>>>
>>>>
>>>> Michael
>>>>
>>>> On Thu, Nov 17, 2011 at 9:44 PM, .Jpg <jporobicg at gmail.com> wrote:
>>>>>
>>>>> Hi everyone, I tried to solve this problem but I could not find the
>>>>> solution. I have about 105 matrices of equal size in the memory of**R,
>>>>> I
>>>>> need to do is extract from these matrices some known positions and
>>>>> create a new matrix with these columns. Show you an example with only
>>>>> three matrices (but in my case I have hundreds of them).
>>>>>
>>>>> A=matrix(1:15,5)
>>>>>
>>>>> [,1] [,2] [,3]
>>>>> [1,] 1 6 11
>>>>> [2,] 2 7 12
>>>>> [3,] 3 8 13
>>>>> [4,] 4 9 14
>>>>> [5,] 5 10 15
>>>>>
>>>>> B=matrix(15:29,5)
>>>>> [,1] [,2] [,3]
>>>>> [1,] 15 20 25
>>>>> [2,] 16 21 26
>>>>> [3,] 17 22 27
>>>>> [4,] 18 23 28
>>>>> [5,] 19 24 29
>>>>>
>>>>> C=matrix(30:44,5)
>>>>> [,1] [,2] [,3]
>>>>> [1,] 30 35 40
>>>>> [2,] 31 36 41
>>>>> [3,] 32 37 42
>>>>> [4,] 33 38 43
>>>>> [5,] 34 39 44
>>>>>
>>>>> The positions I wish to extract are 1 and 5 of the first row of each
>>>>> matrix (in my case are 25positions) and with this generate a new matrix
>>>>> with the form
>>>>>
>>>>> d=
>>>>>
>>>>> [,1] [,2] [,3]
>>>>> [1,] 1 15 30
>>>>> [2,] 5 19 34
>>>>>
>>>>> The ideais to builda loop toextract thisinformation from
>>>>> hundredsmatrices, butI failed todo so.
>>>>>
>>>>> Any helpwould be greatthank you very muchin advance
>>>>>
>>>>> regards
>>>>> .jpg
>>>>>
>>>
>>
>>
>>
>> --
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>>
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> David Winsemius, MD
> West Hartford, CT
>
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list