[R] subsetting a list of matrices

Lara Poplarski larapoplarski at gmail.com
Tue Aug 23 18:28:55 CEST 2011


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



More information about the R-help mailing list