Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Namings | Matrices
Introduction à R : Partie I

bookNamings

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.

      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
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
Tâche

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.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 5
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain how to extract an entire column using column names?

What happens if I try to assign duplicate names to rows or columns?

Can I use names to extract multiple rows or columns at once?

close

Awesome!

Completion rate improved to 2.27

bookNamings

Glissez pour afficher le menu

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.

      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
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
Tâche

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.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

close

Awesome!

Completion rate improved to 2.27
Section 4. Chapitre 5
single

single

some-alt