Hey All, I was wondering if I could solicit a little input on what I'm trying to do here. I have a list of matrices, and I want to set their dimnames, but all I can come up with is this: x <- matrix(1:4,2) y <- matrix(5:8,2) z <- list(x,y) nm <- c("a","b") nms <- list(nm,nm) z <- lapply(z,function(x)dimnames(x)<-nms) As you can see, this doesn't quite work. Could anybody help me, and bonus points if you can help me do this efficiently. Please cc my email address, because I only get summaries of the list. Thanks in advance!
I think what you want is this -- you have to return 'x' from the lapply: x <- matrix(1:4,2) y <- matrix(5:8,2) z <- list(x,y) nm <- c("a","b") nms <- list(nm,nm) z <- lapply(z,function(x){ dimnames(x)<-nms x }) On Thu, May 8, 2008 at 1:33 PM, statmobile <statmobile at gmail.com> wrote:> Hey All, > > I was wondering if I could solicit a little input on what I'm trying to do > here. I have a list of matrices, and I want to set their dimnames, but all > I can come up with is this: > > x <- matrix(1:4,2) > y <- matrix(5:8,2) > > z <- list(x,y) > nm <- c("a","b") > nms <- list(nm,nm) > > z <- lapply(z,function(x)dimnames(x)<-nms) > > As you can see, this doesn't quite work. Could anybody help me, and bonus > points if you can help me do this efficiently. Please cc my email address, > because I only get summaries of the list. > > Thanks in advance! > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
on 05/08/2008 12:33 PM statmobile wrote:> Hey All, > > I was wondering if I could solicit a little input on what I'm trying > to do here. I have a list of matrices, and I want to set their > dimnames, but all I can come up with is this: > > x <- matrix(1:4,2) y <- matrix(5:8,2) > > z <- list(x,y) nm <- c("a","b") nms <- list(nm,nm) > > z <- lapply(z,function(x)dimnames(x)<-nms) > > As you can see, this doesn't quite work. Could anybody help me, and > bonus points if you can help me do this efficiently. Please cc my > email address, because I only get summaries of the list. > > Thanks in advance!The problem is that dimnames() does not return the matrix, so you need to explicitly return 'x': > lapply(z, function(x) {dimnames(x) <- nms; x}) [[1]] a b a 1 3 b 2 4 [[2]] a b a 5 7 b 6 8 HTH, Marc Schwartz