Зміст курсу
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.
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
).
Завдання
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
.
Завдання
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
.
Все було зрозуміло?
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.
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
).
Завдання
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
.
Завдання
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
.
Все було зрозуміло?
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
).
Завдання
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
.
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.
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
).
Завдання
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
.