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

Kurssisisältö

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

book
Namings

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.

python
12345678910
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Assign names of rows rownames(m) <- c('r1', 'r2', 'r3') # Assign names of columns colnames(m) <- c('c1', 'c2', 'c3') m # Output the matrix
copy

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).

12345678
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extact element `4` using namings m["r2", "c1"] # Extract the first row m["r1",]
copy
Tehtävä

Swipe to start coding

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
toggle bottom row

book
Namings

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.

python
12345678910
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Assign names of rows rownames(m) <- c('r1', 'r2', 'r3') # Assign names of columns colnames(m) <- c('c1', 'c2', 'c3') m # Output the matrix
copy

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).

12345678
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extact element `4` using namings m["r2", "c1"] # Extract the first row m["r1",]
copy
Tehtävä

Swipe to start coding

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt