Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Namings | Matrices
course content

Course Content

R Introduction: Part II

NamingsNamings

By this time, we referred to matrix elements by indices. But in the case of large matrices, it will be quite hard to remember and find where precisely necessary elements are.

This issue can be solved by using names on rows/columns. To set names (stored in names vector) of rows for matrix m use rownames(m) <- names. To set names of columns use the same syntax: colnames(m) <- names.

Note

Note that the length of vector names must equal the number of rows or columns respectively. For example, you can not assign 3 column names to a matrix with 4 columns.

For example, let's assign some names to the example matrix.

As you can see, there are names on both rows and columns. If you have names on rows and (or) columns, you can refer to a specific element(s) by using names. You can do it the same way as indexing: specify the name/names of row(s)/column(s) to extract. For example, from the matrix above, we can extract the element 4 (r2 and c1) and the first row (r1).

Task

Remember the task with a local furniture store? Assume we have 3 months of selling data.

MonthSofaArmchairDining tableDining chairBookshelf
March1621302310
April4039132116
May1121363216

This data is stored in the sellings variable without row and column names. Your tasks are:

  1. Assign c("March", "April", "May") to row names of sellings.
  2. Assign c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf") to column names (pay attention to underscore _ characters!).
  3. Output matrix sellings.

Everything was clear?

Section 1. Chapter 5
toggle bottom row
course content

Course Content

R Introduction: Part II

NamingsNamings

By this time, we referred to matrix elements by indices. But in the case of large matrices, it will be quite hard to remember and find where precisely necessary elements are.

This issue can be solved by using names on rows/columns. To set names (stored in names vector) of rows for matrix m use rownames(m) <- names. To set names of columns use the same syntax: colnames(m) <- names.

Note

Note that the length of vector names must equal the number of rows or columns respectively. For example, you can not assign 3 column names to a matrix with 4 columns.

For example, let's assign some names to the example matrix.

As you can see, there are names on both rows and columns. If you have names on rows and (or) columns, you can refer to a specific element(s) by using names. You can do it the same way as indexing: specify the name/names of row(s)/column(s) to extract. For example, from the matrix above, we can extract the element 4 (r2 and c1) and the first row (r1).

Task

Remember the task with a local furniture store? Assume we have 3 months of selling data.

MonthSofaArmchairDining tableDining chairBookshelf
March1621302310
April4039132116
May1121363216

This data is stored in the sellings variable without row and column names. Your tasks are:

  1. Assign c("March", "April", "May") to row names of sellings.
  2. Assign c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf") to column names (pay attention to underscore _ characters!).
  3. Output matrix sellings.

Everything was clear?

Section 1. Chapter 5
toggle bottom row
some-alt