Re: [R] Help in splitting a list

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Tue, 05 Apr 2011 08:53:16 -0300

Try this:

mapply('[', lapply(mylist, as.data.frame), c(index, lapply(index, `!`)), SIMPLIFY = FALSE) On Tue, Apr 5, 2011 at 7:46 AM, Lars Bishop <lars52r_at_gmail.com> wrote:
> 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,
> Lars.
>
>        [[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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
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 Tue 05 Apr 2011 - 11:56:31 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 Tue 05 Apr 2011 - 13:20:26 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