Transferred from: http://www.dataguru.cn/article-2217-1.html
The process of matrix (tabular) data is often used in R languages, and it is particularly important to define a name for its ranks in the complex data. Batch naming is a good way to handle huge amounts of data, and here are some concrete examples of how to name the columns of matrices in the R language in batches.
> |
x <-Matrix (1:12,nrow=3,byrow=t) |
Initializes a matrix that fills in the order of the preceding columns |
> |
X |
View Matrix x |
|
[, 1] [, 2] [, 3] [, 4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 |
|
> |
Rownames (x) <-Letters[1:3] |
Use the first three uppercase letters to name the lines in turn |
> |
X |
View Matrix x |
|
[, 1] [, 2] [, 3] [, 4] A 1 2 3 4 B 5 6 7 8 C 9 10 11 12 |
|
> |
Colnames (x) <-C ("X", "Y", "Doc", "Bear") |
Use vectors with equal capacity and number of columns to name each column |
> |
X |
View Matrix x |
|
X Y Doc Bear A 1 2 3 4 B 5 6 7 8 C 9 10 11 12 |
|
|
|
|
> |
M0 <-Matrix (NA, 4, 0) |
Initializes a matrix containing 4 rows with no padding for the cells |
> |
M0 |
View the M0 |
|
[1,] [2,] [3,] [4,] |
|
> |
Rownames (M0) Null |
Query row name |
|
|
|
> |
M2 <-cbind (1,1:4) |
Initial data frame, each column is filled with different rules |
> |
M2 |
View m2 |
|
[, 1] [, 2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 |
|
> |
Colnames (M2, do. NULL = FALSE) |
Returns the system default column name |
|
[1] "col1" "col2" |
|
|
Colnames (m2) <-C ("X", "Y") |
Use vectors to name columns |
> |
M2 |
View m2 |
|
X Y [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 |
|
> |
Rownames (m2) <-rownames (M2, do. NULL = FALSE, prefix = "bear.") |
Name each row using the prefix increment method |
> |
M2 |
View m2 |
|
X Y Bear.1 1 1 Bear.2 1 2 BEAR.3 1 3 BEAR.4 1 4 |
|
> |
Nam=colnames (m2) |
Create a column name vector by assigning a column name to the vector Nam |
Use the R language to name the rows and columns of a matrix (table)