[R] Help in splitting a list
Henrique Dallazuanna
wwwhsd at gmail.com
Tue Apr 5 13:53:16 CEST 2011
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
More information about the R-help
mailing list