[R] Using Matrix of Names for Indexing an Array

GREGOR Brian J Brian.J.GREGOR at odot.state.or.us
Fri Mar 24 19:10:33 CET 2006

Both "An Introduction to R" (Section 5.3) and "R Language Definition"
(Section 3.4.2) describe how to use a matrix of integers to index
arrays. The "R Language Definition" seems to say that a matrix of names
could be used as well:

"Both in the case of using a single index and in matrix indexing, a
names attribute is used if present, as had the structure been
one-dimensional."(Section3.4.2, page 16)

A matrix of names does not seem to work. Here is an example:

> Zn <- c("100", "101", "102", "103", "104")
> Something.ZnZn <- matrix(1:25, length(Zn), length(Zn),
dimnames=list(Zn, Zn))
> NameIndex.Zn2 <- cbind(Zn, rev(Zn))
> PositionIndex.Zn2 <- apply(NameIndex.Zn2, 2, function(x) match(x, Zn))
> Something.ZnZn[PositionIndex.Zn2]
[1] 21 17 13  9  5
> Something.ZnZn[NameIndex.Zn2]
<NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 
  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA

Am I misunderstanding what is being said in this sentence or am I
implementing it incorrectly?

Brian Gregor, P.E.
Transportation Planning Analysis Unit
Oregon Department of Transportation
Brian.J.GREGOR at odot.state.or.us
(503) 986-4120

More information about the R-help mailing list