Folks:

David: I believe your approach needs to be modified to accommodate
hundreds of matrices per the OP's specification.

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

> 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 <dwinsem...@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 <jporob...@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
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help@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.
>>>
>>
>> ______________________________________________
>> R-help@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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help@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.
>



-- 

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

______________________________________________
R-help@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.

Reply via email to