Dear list, I have a list of matrices : i1 <- matrix(1:10, nrow = 2, ncol = 5) i2 <- matrix(11:20, nrow = 2, ncol = 5) j <- list(i1 = i1, i2 = i2) I would like to attribute names to each dimension, for each matrix, as follows : $i1 B1 B2 B3 B4 B5 A1 1 3 5 7 9 A2 2 4 6 8 10 $i2 B1 B2 B3 B4 B5 A1 11 13 15 17 19 A2 12 14 16 18 20 However, I have to use lapply function and attribute names after declaration of j, like this : names <- list(c("A1", "A2"), c("B1", "B2", "B3", "B4", "B5")) lapply(j, function (x) {dimnames(x) <- names}) or lapply(j, function (x) {as.matrix(x, dimnames = names)}) But, it does not work. I can use : lapply(j, function (x) {matrix(x, nrow = 2, ncol = 5, dimnames names)}) But, I think a better way exists ... Thanks in advance, Carlos [[alternative HTML version deleted]]
Hi Carlos, Look at this: i1 <- matrix(1:10, nrow = 2, ncol = 5) i2 <- matrix(11:20, nrow = 2, ncol = 5) j <- list(i1 = i1, i2 = i2) my.names <- list(c("A1", "A2"), c("B1", "B2", "B3", "B4", "B5")) j.new <- lapply(j, function (x) {dimnames(x) <- my.names; return(x)}) j.new the issue was just that you have to save the output from lapply. Cheers, Josh On Tue, Aug 3, 2010 at 8:42 AM, Carlos Petti <carlos.petti at gmail.com> wrote:> Dear list, > > I have a list of matrices : > > i1 <- matrix(1:10, nrow = 2, ncol = 5) > i2 <- matrix(11:20, nrow = 2, ncol = 5) > j <- list(i1 = i1, i2 = i2) > > I would like to attribute names to each dimension, for each matrix, > as follows : > > $i1 > B1 B2 B3 B4 B5 > A1 1 3 5 7 9 > A2 2 4 6 8 10 > > $i2 > B1 B2 B3 B4 B5 > A1 11 13 15 17 19 > A2 12 14 16 18 20 > > However, I have to use lapply function and attribute names > after declaration of j, like this : > > names <- list(c("A1", "A2"), c("B1", "B2", "B3", "B4", "B5")) > lapply(j, function (x) {dimnames(x) <- names}) > or > lapply(j, function (x) {as.matrix(x, dimnames = names)}) > > But, it does not work. > > I can use : lapply(j, function (x) {matrix(x, nrow = 2, ncol = 5, dimnames > names)}) > > But, I think a better way exists ... > > Thanks in advance, > Carlos > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Gabor Grothendieck
2010-Aug-03 16:00 UTC
[R] How to name matrices from a list with lapply ?
On Tue, Aug 3, 2010 at 11:42 AM, Carlos Petti <carlos.petti at gmail.com> wrote:> Dear list, > > I have a list of matrices : > > i1 <- matrix(1:10, nrow = 2, ncol = 5) > i2 <- matrix(11:20, nrow = 2, ncol = 5) > j <- list(i1 = i1, i2 = i2) > > I would like to attribute names to each dimension, for each matrix, > as follows : > > $i1 > B1 B2 B3 B4 B5 > A1 1 3 5 7 9 > A2 2 4 6 8 10 > > $i2 > B1 B2 B3 B4 B5 > A1 11 13 15 17 19 > A2 12 14 16 18 20 > > However, I have to use lapply function and attribute names > after declaration of j, like this : > > names <- list(c("A1", "A2"), c("B1", "B2", "B3", "B4", "B5")) > lapply(j, function (x) {dimnames(x) <- names}) > or > lapply(j, function (x) {as.matrix(x, dimnames = names)}) > > But, it does not work. > > I can use : lapply(j, function (x) {matrix(x, nrow = 2, ncol = 5, dimnames > names)}) >Try this: lapply(j, structure, dimnames = names)