1

In this question the object x (pasted below) is an array*. x has an argument called .Dimnames and another called .Names, which seems to name each grouping of .Dimnames. When x is converted to a data.frame the values in .Names become the column names.

.Dimnames seems to be analogous to array(dimnames), but I can't find the analogous array argument for .Names.

# object from the linked question

x = structure(c(-0.93, 0.39, 0.88, 0.63, 0.86, -0.69, 1.02, 0.29, 0.94, 
0.93, -0.01, 0.79, 0.32, 0.14, 0.13, -0.07, -0.63, 0.26, 0.07, 0.87,
-0.36, 1.043, 0.33, -0.12, -0.055, 0.07, 0.67, 0.48, 0.002, 0.008, 
-0.19, -1.39, 0.98, 0.43, -0.02, -0.15,-0.08, 0.74, 0.96, 0.44, -0.005,
1.09, 0.36, 0.04, 0.09, 0.17, 0.68, 0.51, 0.09, 0.12, -0.05, 0.11,
0.99, 0.62, 0.13, 0.06, 0.27, 0.74, 0.96, 0.45), .Dim = c(5L, 
2L, 2L, 3L), .Dimnames = structure(list(Subject = c("s1", "s2", 
"s3", "s4", "s5"), Cond = c("A", "B"), Item = c("1", "2"), c("Measure1", 
"Measure2", "Measure3")), .Names = c("Subject", "Cond", 
"Item", "")))

When I convert x using as.data.frame.table the column names are meaningful. E.g.

head(as.data.frame.table(x))

#-----
  Subject Cond Item     Var4  Freq
1      s1    A    1 Measure1 -0.93
2      s2    A    1 Measure1  0.39
3      s3    A    1 Measure1  0.88
4      s4    A    1 Measure1  0.63
5      s5    A    1 Measure1  0.86
6      s1    B    1 Measure1 -0.69

If I have another array y, how do I name a group of dimensions so that when I convert to a data.frame the column names are meaningful? E.g

y <- array(1:9,
      dim = c(3,3,3),
      dimnames = list(letters[1:3],
                      LETTERS[1:3],
                      month.abb[1:3]))

as.data.frame.table(y)

#---------
  Var1 Var2 Var3 Freq
1    a    A  Jan    1
2    b    A  Jan    2
3    c    A  Jan    3
4    a    B  Jan    4
5    b    B  Jan    5
6    c    B  Jan    6

*Note: str(x) does not say array, but x behaves like an array. Maybe I need to convert to a different data structure?

nniloc
  • 4,128
  • 2
  • 11
  • 22
  • Can you explain what you mean by *"column names are meaningful"*? I think that `Subject`, `Cond`, `Item`, or `Freq` are meaningful enough ... only `Var4` seems uninformative to me. – r2evans Jul 08 '20 at 22:34
  • Sorry for not being clear. I am just using `x` as an example. How would I apply names like `Subject` or `Cond` to a different array? – nniloc Jul 08 '20 at 22:36
  • 1
    `ary <- array(1:24, dim=c(2,3,4)); dimnames(ary) <- list(a=c(),b=c(),d=c())`? Perhaps I'm missing something, my apologies. – r2evans Jul 08 '20 at 22:38
  • 1
    Ah, simple fix. Didn't realize you could put a name on the left hand side of an `=` in `dimnames`. If you wanted to put your comment as an answer I'll accept it. – nniloc Jul 08 '20 at 22:45

1 Answers1

1

You can assign names of the dimensions without naming the actual rows/columns/pipes. (I think the 3rd margin is called a "pipe", not to be confused with %>% which is not a pipe ;-)

ary <- array(1:24, dim=c(2,3,4))
dimnames(ary) <- list(a=c(),b=c(),d=c())
ary
# , , 1
#       b
# a      [,1] [,2] [,3]
#   [1,]    1    3    5
#   [2,]    2    4    6
# , , 2
#       b
# a      [,1] [,2] [,3]
#   [1,]    7    9   11
#   [2,]    8   10   12
# , , 3
#       b
# a      [,1] [,2] [,3]
#   [1,]   13   15   17
#   [2,]   14   16   18
# , , 4
#       b
# a      [,1] [,2] [,3]
#   [1,]   19   21   23
#   [2,]   20   22   24
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Defining `a`, `b` and `d` in your example is what I was looking for. After you pointed it out I now see in the [documentation](https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/array) for `dimname` says *The list can be named, and the list names will be used as names for the dimensions*. – nniloc Jul 08 '20 at 23:03