在R中更改矩阵和数据帧的暗名 [英] Changing dimnames of matrices and data frames in R
问题描述
假设我创建了以下矩阵:
Let's say I have created the following matrix:
> x <- matrix(1:20000,nrow=100)
> x[1:10,1:10]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 101 201 301 401 501 601 701 801 901
[2,] 2 102 202 302 402 502 602 702 802 902
[3,] 3 103 203 303 403 503 603 703 803 903
[4,] 4 104 204 304 404 504 604 704 804 904
[5,] 5 105 205 305 405 505 605 705 805 905
[6,] 6 106 206 306 406 506 606 706 806 906
[7,] 7 107 207 307 407 507 607 707 807 907
[8,] 8 108 208 308 408 508 608 708 808 908
[9,] 9 109 209 309 409 509 609 709 809 909
[10,] 10 110 210 310 410 510 610 710 810 910
R中更改行和列名称的方法有哪些?例如,我希望行名称为 SS1,SS2,...,SS100
,列名称为 M1,M2,...,M200
.我通常使用具有数千行和列的数据,并且我需要一个好的方法来做到这一点.有些人使用 attributes(x)$ dimnames<-list(...)
之类的东西,有些人使用 rownames<-paste(...)
.有什么可能的方法?
What are the methods in R to change row and column names? For example, I like row names to be SS1, SS2, ..., SS100
and column names to be M1, M2, ..., M200
. I usually work with data with 1000s of rows and columns, and I need a good method to do that. Some people use something like attributes(x)$dimnames <- list(...)
and some use rownames <- paste(...)
. What are all possible methods?
我的第二个问题是,在将矩阵转换为数据帧之后,可以使用相同的方法吗?
My second question is, can I use the same methods after I convert the matrix to a data frame?
推荐答案
从评论到答案:
row.names(x) <- paste("SS", 1:nrow(x), sep="")
colnames(x) <- paste("M" , 1:ncol(x), sep="")
@doug写道,它适用于矩阵和数据帧.
As @doug wrote, it works for matrices and data frames.
这篇关于在R中更改矩阵和数据帧的暗名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!