2

I've created a list of matrices in R. In all matrices in the list, I'd like to "pull out" the collection of matrix elements of a particular index. I was thinking that the colon operator might allow me to implement this in one line. For example, here's an attempt to access the [1,1] elements of all matrices in a list:

myList = list() #list of matrices
myList[[1]] = matrix(1:9, nrow=3, ncol=3, byrow=TRUE) #arbitrary data
myList[[2]] = matrix(2:10, nrow=3, ncol=3, byrow=TRUE)

#I expected the following line to output myList[[1]][1,1], myList[[2]][1,1]
slice = myList[[1:2]][1,1] #prints error: "incorrect number of dimensions"

The final line of the above code throws the error "incorrect number of dimensions."

For reference, here's a working (but less elegant) implementation of what I'm trying to do:

#assume myList has already been created (see the code snippet above)
slice = c()
for(x in 1:2) {
    slice = c(slice, myList[[x]][1,1])
}
#this works. slice = [1 2]

Does anyone know how to do the above operation in one line?

Note that my "list of matrices" could be replaced with something else. If someone can suggest an alternative "collection of matrices" data structure that allows me to perform the above operation, then this will be solved.

Perhaps this question is silly...I really would like to have a clean one-line implementation though.

3 Answers 3

4

Two things. First, the difference between [ and [[. The relevant sentence from ?'[':

The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.

So you probably want to do myList[1:2]. Second, you can't combine subsetting operations in the way you describe. Once you do myList[1:2] you will get a list of two matrices. A list typically has only one dimension, so doing myList[1:2][1,1] is nonsensical in your case. (See comments for exceptions.)

You might try lapply instead: lapply(myList,'[',1,1).

2
  • Totally unrelated to the question, but in response to " A list has only one dimension": You can give a list more dimensions with dim(). e.g. foo <- list(1,2,3,4);dim(foo) <- c(2,2);foo[1,2];foo[[1,2]]. Sometimes this can be useful. May 10, 2012 at 13:10
  • myList[1:2][1,1] is still nonsensical though as myList[1:2] will return a list with only one dimension :) May 10, 2012 at 13:17
4

If your matrices will all have same dimension, you could store them in a 3-dimensional array. That would certainly make indexing and extracting elements easier ...

## One way to get your data into an array
a <- array(c(myList[[1]], myList[[2]]), dim=c(3,3,2))

## Extract the slice containing the upper left element of each matrix
a[1,1,]
# [1] 1 2
2

This works:

> sapply(myList,"[",1,1)
[1] 1 2

edit: oh, sorry, I see almost the same idea toward the end of an earlier answer. But sapply probably comes closer to what you want, anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.