retrieve the title information from different dimensions based on str()

Viewed 25

In the simulation, it generates a variable with following information. The last two lines of information of str() is .$ : NULL. What does it mean? Moreover, ..$ : chr [1:4] "E avg" "stat avg" "Angle" "int" should correspond to the names in the first dimension of this array. What is the function that can retrieve these names from xy_log?

> class(xy_log)
[1] "array"
> dim(xy_log)
[1]    4 2272 1000
> str(xy_log)
 num [1:4, 1:2272, 1:1000] 1.02e+06 2.52e-01 3.20e+01 3.99e+03 3.15e+06 ...
 - attr(*, "dimnames")=List of 3
  ..$ : chr [1:4] "E avg" "stat avg" "Angle" "int"
  ..$ : NULL
  ..$ : NULL
2 Answers

I think you're looking for dimnames(xy_log)[[1]] (for the names of the first dimension)

x <- array(1:8,dim=c(2,2,2),
       dimnames=list(c("a","b"), c("A","B"), c("i","ii")))
dimnames(x)[[1]]
## [1] "a" "b"

More generally you could use attr(xy_log, "dimnames")[[1]], but dimnames() is better since it's more specific.

The NULL values mean that the second and third dimensions are unnamed.

rownames(xy_log) ## should get them

Though rows and columns are typically associated with a 2D-structure, and with for example 3 dimensions as you have with your array, dimnames which Ben uses, which retrieves all names along all dimensions, might be clearer.

The meaning of:

$ : NULL

Is simply that your data has no names along this dimensions. So your 4 rows has a name associated with each row, whereas your columns and your 3rd dimension has no names similarly associated

(Note - I hardly ever use the str() function to grasp useful information about my data, there are just many other more useful functions available that are more to the point), so in your case:

  ..$ : chr [1:4] "E avg" "stat avg" "Angle" "int"     ## <-- these are your rownames
  ..$ : NULL                                           ## <-- these would have been your column names, had you had any
  ..$ : NULL                                           ## <-- these would have been your '3rd-dimension' names, had they had any

You can also use these names to subset your data, althouh in case of repeated similar names, this may lead to nasty unforseen bugs so I wouldn't really recomend it.

xy_log[ "E avg",, ] ## gets all data from this one row (which would give you a matrix in this case)
Related