Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 73

4.3.5.2 Naming Elements of Arrays

Оглавление

In most applications it will be enough, to refer to an element in an array by its number. However naming elements makes code easier to read and can make it more robust. For example, if we change the array definition, the numbers of its elements can change, but the name will still be a valid reference.

# Create two vectors: v1 <- c(1,1) v2 <- c(10:13) col.names <- c("col1","col2", "col3") row.names <- c("R1","R2") matrix.names <- c("Matrix1","Matrix2") # Take these vectors as input to the array. a <- array(c(v1,v2),dim = c(2,3,2), dimnames = list(row.names,col.names, matrix.names)) print(a) # This allows to address the first row in Matrix 1 as follows: a[‘R1’,,‘Matrix1’]

The Big R-Book

Подняться наверх