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

4.3.4.2 Naming Rows and Columns

Оглавление

While in general naming rows and/or columns is more relevant for datasets than matrices it is possible to work with matrices to store data if it only contains one type of variable.

row_names = c("row1", "row2", "row3", "row4") col_names = c("col1", "col2", "col3") M <- matrix(c(10:21), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names)) print(M) ## col1 col2 col3 ## row1 10 11 12 ## row2 13 14 15 ## row3 16 17 18 ## row4 19 20 21

dimnames

Once thematrix exists, the columns and rows can be renamed with the functions colnames() and rownames()

colnames()

rownames()

colnames(M) <- c(‘C1’, ‘C2’, ‘C3’) rownames(M) <- c(‘R1’, ‘R2’, ‘R3’, ‘R4’) M ## C1 C2 C3 ## R1 10 11 12 ## R2 13 14 15 ## R3 16 17 18 ## R4 19 20 21

The Big R-Book

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