Use the R language to name the rows and columns of a matrix (table)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.