Labeling Rows and Columns
Instead of referring to matrix elements by numeric indices, you can assign names to rows and columns. This makes large matrices easier to read and navigate.
Assigning Names
You can use rownames() and colnames() functions to add labels to rows and columns.
Example
12345678m <- matrix(1:9, nrow = 3, byrow = TRUE) # Assign row names rownames(m) <- c("r1", "r2", "r3") # Assign column names colnames(m) <- c("c1", "c2", "c3") m
The number of names must match the number of rows or columns in the matrix.
Accessing by Names
Once names are assigned, you can extract elements or entire rows/columns using them.
Example
123456789num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extract element at row "r2", column "c1" (value 4) m["r2", "c1"] # Extract the entire first row m["r1",]
Using names instead of indices makes code more readable and less error-prone.
Swipe to start coding
You are given a matrix sellings that stores sales data for a local furniture store across three months:
| Month | Sofa | Armchair | Dining table | Dining chair | Bookshelf |
|---|---|---|---|---|---|
| March | 16 | 21 | 30 | 23 | 10 |
| April | 40 | 39 | 13 | 21 | 16 |
| May | 11 | 21 | 36 | 32 | 16 |
The matrix currently lacks row and column names.
Your tasks are to:
- Assign
c("March", "April", "May")to row names ofsellings. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")to column names. Pay attention: use underscore (_) characters instead of spaces. - Output matrix
sellings.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 2.27
Labeling Rows and Columns
Svep för att visa menyn
Instead of referring to matrix elements by numeric indices, you can assign names to rows and columns. This makes large matrices easier to read and navigate.
Assigning Names
You can use rownames() and colnames() functions to add labels to rows and columns.
Example
12345678m <- matrix(1:9, nrow = 3, byrow = TRUE) # Assign row names rownames(m) <- c("r1", "r2", "r3") # Assign column names colnames(m) <- c("c1", "c2", "c3") m
The number of names must match the number of rows or columns in the matrix.
Accessing by Names
Once names are assigned, you can extract elements or entire rows/columns using them.
Example
123456789num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extract element at row "r2", column "c1" (value 4) m["r2", "c1"] # Extract the entire first row m["r1",]
Using names instead of indices makes code more readable and less error-prone.
Swipe to start coding
You are given a matrix sellings that stores sales data for a local furniture store across three months:
| Month | Sofa | Armchair | Dining table | Dining chair | Bookshelf |
|---|---|---|---|---|---|
| March | 16 | 21 | 30 | 23 | 10 |
| April | 40 | 39 | 13 | 21 | 16 |
| May | 11 | 21 | 36 | 32 | 16 |
The matrix currently lacks row and column names.
Your tasks are to:
- Assign
c("March", "April", "May")to row names ofsellings. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")to column names. Pay attention: use underscore (_) characters instead of spaces. - Output matrix
sellings.
Lösning
Tack för dina kommentarer!
single