-1

I have a existing array that I made and I want to name the dimensions of this array. I can't use the dimnames= argument of array() because I need to make this array with a different function. I need to rename the dimensions with something similar to this names(my.array)<-my.names.

Thanks for the help (and I'm new to this if you can't already tell)

4

2 回答 2

5

使用dimnames(x) <- list(d1names, d2names, ...)whered1namesd2names是长度与维度长度匹配的字符向量。

如果您的数组是二维的(矩阵),则可以使用rownames(x) <- d1namesandcolnames(x) <- d2names代替。

例子:

> A <- outer(outer(1:3,1:4),1:2)
> A
, , 1

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    2    4    6    8
[3,]    3    6    9   12

, , 2

     [,1] [,2] [,3] [,4]
[1,]    2    4    6    8
[2,]    4    8   12   16
[3,]    6   12   18   24

> dimnames(A)
NULL
> dimnames(A) <- list(LETTERS[1:3],LETTERS[1:4],LETTERS[1:2])
> A
, , A

  A B C  D
A 1 2 3  4
B 2 4 6  8
C 3 6 9 12

, , B

  A  B  C  D
A 2  4  6  8
B 4  8 12 16
C 6 12 18 24

矩阵示例:

> B <- matrix(1:12,3,4)
> B
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> rownames(B) <- letters[1:3]
> B
  [,1] [,2] [,3] [,4]
a    1    4    7   10
b    2    5    8   11
c    3    6    9   12
> colnames(B) <- LETTERS[1:4]
> B
  A B C  D
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12
于 2013-03-31T01:50:33.450 回答
1

你仍然可以使用dimnames<- 例如:

someArray <- array(1:30, dim=c(2, 3, 5))

dimnames(someArray) <- list(c("Hello", "World"), LETTERS[6:8], letters[1:5])

someArray

  # , , a

  #       F G H
  # Hello 1 3 5
  # World 2 4 6

  # , , b

  #       F  G  H
  # Hello 7  9 11
  # World 8 10 12

  # , , c

  #        F  G  H
  # Hello 13 15 17
  # World 14 16 18

  # , , d

  #        F  G  H
  # Hello 19 21 23
  # World 20 22 24

  # , , e

  #        F  G  H
  # Hello 25 27 29
  # World 26 28 30
于 2013-03-31T01:53:15.313 回答