[R] extract positions from matrix
David Winsemius
dwinsemius at comcast.net
Sat Nov 19 18:19:42 CET 2011
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
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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 at 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
More information about the R-help
mailing list