[R] subsetting a list of matrices

Henrique Dallazuanna wwwhsd at gmail.com
Tue Aug 23 18:35:22 CEST 2011


Try this:

subset(as.data.frame(do.call(rbind, lapply(l, "[", , 1))), row3 == "Message 1")

On Tue, Aug 23, 2011 at 1:28 PM, Lara Poplarski <larapoplarski at gmail.com> wrote:
> Hi all,
>
> I have an object that looks (roughly) like the following:
>
> l <- list(a = matrix(rnorm(9), 3), b = matrix(rnorm(9), 3), c =
> matrix(rnorm(9), 3))
>
> l$a[3,] <- sample(c("Message 1", "Message 2", "Message 3"))
> l$b[3,] <- sample(c("Message 1", "Message 2", "Message 3"))
> l$c[3,] <- sample(c("Message 1", "Message 2", "Message 3"))
>
> rownames(l$a) <- rownames(c(1:3), do.NULL = FALSE, prefix = "row")
> rownames(l$b) <- rownames(c(1:3), do.NULL = FALSE, prefix = "row")
> rownames(l$c) <- rownames(c(1:3), do.NULL = FALSE, prefix = "row")
>
> colnames(l$a) <- c("V1", "V2", "V3")
> colnames(l$b) <- c("V1", "V2", "V3")
> colnames(l$c) <- c("V1", "V2", "V3")
>
> I want to extract values (row1, V1) for the three sublists a, b, c,
> but only for those cases in which row3 == "Message 1". Could someone
> suggest how to proceed?
>
> Many thanks in advance,
> Lara
>
> ______________________________________________
> 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