[R] Help in sub-setting a List

From: Axel Urbiz <axel.urbiz_at_gmail.com>
Date: Mon, 04 Apr 2011 18:57:48 -0400


Dear R users,
Let's say I have a list with components being 'm' matrices (as exemplified in the "mylist" object below). Now, I'd like to subset this list based on an index vector, which will partition each matrix 'm' in 2 sub-matrices. My questions are:

  1. Is there an elegant way to have the results shown in mylist2 for an arbitrary number of matrices in mylist?
  2. The column names are 'lost' for mylist2[[2]] and mylist2[[4]] (but not for mylist2[[1]] and mylist2[[3]]). Is there a way to keep the column names in the results of mylist2?

mylist <- list(matrix(1:9,3,3), matrix(10:18,3,3)) colnames(mylist[[1]])=c('x1','x2','x3')
colnames(mylist[[2]])=c('x4','x5','x6')
index <- list(2)

index[[1]] <- c(TRUE,FALSE,TRUE)
index[[2]] <- c(FALSE,TRUE,TRUE)
mylist2 <- list(as.matrix(mylist[[1]][,index[[1]]]),
                as.matrix(mylist[[1]][,!index[[1]]]),
                as.matrix(mylist[[2]][,index[[2]]]),
                as.matrix(mylist[[2]][,!index[[2]]]))

Thanks for any help,

Axel.

        [[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. Received on Mon 04 Apr 2011 - 22:59:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 23:40:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive