Skip to content
Prev 277929 / 398506 Next

extract positions from matrix

On Nov 19, 2011, at 9:32 AM, R. Michael Weylandt wrote:

            
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.