[R] naming rows/columns in 'array of matrices' | solved

David L Carlson dcarlson at tamu.edu
Sat Jan 31 21:11:25 CET 2015


You can also add names to the dimensions:

> dimnames(P)[[1]] <- c("live","dead")
> dimnames(P)[[2]] <- c("old","young")
> names(dimnames(P)) <- c("status", "age", NULL)
> P
, , 1

      age
status old young
  live   1     2
  dead   3     4

, , 2

      age
status old young
  live   5     6
  dead   7     8


David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of peter dalgaard
Sent: Saturday, January 31, 2015 2:19 AM
To: Evan Cooch
Cc: r-help at r-project.org
Subject: Re: [R] naming rows/columns in 'array of matrices' | solved


> On 30 Jan 2015, at 20:34 , Evan Cooch <evan.cooch at gmail.com> wrote:
> 
> The (obvious, after the fact) solution at the bottom. D'oh...
> 
[snip]
> Forgot I was dealing with a multi-dimensional array, not a list. So, following works fine. I'm sure there are better approaches (where 'better' is either 'cooler', or 'more flexible'), but for the moment...)
> 
> P <- array(0, c(2,2,2),dimnames=list(c("live","dead"),c("old","young"),NULL))
> 
> P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T);
> P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T);
> 
> print(P);
> 

Just for completeness, this also works:

> P <- array(0, c(2,2,2)) 
> P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T);
> P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T);

> dimnames(P)[[1]] <- c("live","dead")
> dimnames(P)[[2]] <- c("live","dead")


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list