Naming array dimensions gives error: length of 'dimnames' not equal to array extent
Asked Answered
H

2

11

This is my first time using a 3 dimensional array and I am having problems about naming the third dimension.

ReplicateData <- array(0, c(240, 500, 5), dimnames=list(NULL, NULL, c("Returns", "Replicates", "Asset Class")))

I am getting the error:

Length of dimnames not equal to array extent

This seems like it should be a simple issue but I can't find an explicit example in the help docs or online.

Hysteric answered 9/7, 2010 at 2:45 Comment(0)
H
13

The third dimension of your array is of extent 5, but the vector of names for that dimension is of length three.

Hardener answered 9/7, 2010 at 3:3 Comment(2)
Thanks. I was thinking that the name length should match the number of dimensions.Hysteric
Well, that's something you can have, too; something we call "named dimnames". The table() function actually is an important example using them: > with(airquality, table(OzHi=Ozone > 80, Month, useNA="ifany")) Month OzHi 5 6 7 8 9 FALSE 25 9 20 19 27 TRUE 1 0 6 7 2 <NA> 5 21 5 5 1 You'd manually use something like tab. <- array(1:15, dim=c(3,5), dimnames= list(OzHi = c("FALSE", "TRUE", NA), Month = c("5", "6", "7", "8", "9"))) to get the "same" [why on earth, can't I get "markdown" to work?]Bickering
N
8

Since I understand answers better with a bit 'o code to guide me... here is Jonathan Chang's correct answer translated to code:

ReplicateData <- array(0,c(240,500,5),dimnames=list(NULL, NULL, 
  c("Returns","Replicates","Asset Class", "Fourth Dimname", "Fifth Dimname")))
Nablus answered 7/1, 2015 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.