Conteúdo do Curso
R Introduction: Part II
R Introduction: Part II
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.
# 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
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
).
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",]
Tarefa
Remember the task with a local furniture store? Assume we have 3 months of selling data.
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 |
This data is stored in the sellings
variable without row and column names. Your tasks are:
- Assign
c("March", "April", "May")
to row names ofsellings
. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")
to column names (pay attention to underscore_
characters!). - Output matrix
sellings
.
Obrigado pelo seu feedback!
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.
# 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
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
).
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",]
Tarefa
Remember the task with a local furniture store? Assume we have 3 months of selling data.
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 |
This data is stored in the sellings
variable without row and column names. Your tasks are:
- Assign
c("March", "April", "May")
to row names ofsellings
. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")
to column names (pay attention to underscore_
characters!). - Output matrix
sellings
.
Obrigado pelo seu feedback!
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.
# 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
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
).
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",]
Tarefa
Remember the task with a local furniture store? Assume we have 3 months of selling data.
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 |
This data is stored in the sellings
variable without row and column names. Your tasks are:
- Assign
c("March", "April", "May")
to row names ofsellings
. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")
to column names (pay attention to underscore_
characters!). - Output matrix
sellings
.
Obrigado pelo seu feedback!
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.
# 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
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
).
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",]
Tarefa
Remember the task with a local furniture store? Assume we have 3 months of selling data.
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 |
This data is stored in the sellings
variable without row and column names. Your tasks are:
- Assign
c("March", "April", "May")
to row names ofsellings
. - Assign
c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf")
to column names (pay attention to underscore_
characters!). - Output matrix
sellings
.