尝试更改矩阵中的列标题名称,但出现一些奇怪的现象(在R中)

时间:2019-06-01 23:32:43

标签: r

我有一个30行4列的矩阵(我称之为“ y”)。现在,列名称为NULL。我正在尝试更改各个列的名称,但效果不佳。

我尝试单独重命名每列(创建矩阵后,请参见下文)。

y <- t(replicate(30, diff(c(0, sort(runif(3)), 100))))
names(y)[1] <- "very small"
names(y)[2] <- "small"
names(y)[3] <- "medium"
names(y)[4] <- "large"

这是我的矩阵,但标题不起作用:

         [,1]       [,2]       [,3]     [,4]
[1,] 0.160791460 0.19036607 0.12580291 99.52304
[2,] 0.494404672 0.37335411 0.12121969 99.01102
[3,] 0.322312759 0.07680743 0.13229986 99.46858
[4,] 0.320615717 0.02913797 0.48735808 99.16289
[5,] 0.250489308 0.20835271 0.49417666 99.04698
[6,] 0.065639576 0.62845967 0.11742292 99.18848
[7,] 0.318023945 0.09396768 0.25217427 99.33583
[8,] 0.109697064 0.26258440 0.43483501 99.19288
[9,] 0.438948497 0.23642842 0.31614101 99.00848
[10,] 0.061270783 0.44413492 0.05874163 99.43585
[11,] 0.587190304 0.07693372 0.07609361 99.25978
[12,] 0.097498817 0.05573392 0.34773439 99.49903
[13,] 0.418862582 0.04924732 0.25642872 99.27546
[14,] 0.313116374 0.02291056 0.43163056 99.23234
[15,] 0.167363657 0.28980591 0.10015846 99.44267
[16,] 0.003417506 0.38710120 0.34930292 99.26018
[17,] 0.232000787 0.30794160 0.06402202 99.39604
[18,] 0.233561779 0.35227333 0.36642543 99.04774
[19,] 0.396694646 0.11903936 0.41165674 99.07261
[20,] 0.147861399 0.13515116 0.59463398 99.12235
[21,] 0.469082454 0.03757156 0.29131755 99.20203
[22,] 0.008113136 0.53687528 0.35030648 99.10471
[23,] 0.097810979 0.24581902 0.07533917 99.58103
[24,] 0.666495166 0.09177066 0.20840372 99.03333
[25,] 0.198825197 0.03246302 0.14705370 99.62166
[26,] 0.460175968 0.17809558 0.34293910 99.01879
[27,] 0.349538516 0.50323914 0.13838036 99.00884
[28,] 0.498382490 0.18309442 0.17280276 99.14572
[29,] 0.817413253 0.08962883 0.02642045 99.06654
[30,] 0.139955595 0.30773060 0.22649081 99.32582

相反,在我的矩阵正下方,我得到这样的东西:

attr(,"names")
[1] "very small" "small"      "medium"     "large"      NA           NA           
NA           NA           NA           NA          
[11] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[21] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[31] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[41] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[51] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[61] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[71] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[81] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[91] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[101] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA          
[111] NA           NA           NA           NA           NA           NA           
NA           NA           NA           NA

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用colnames

y <- t(replicate(30, diff(c(0, sort(runif(3)), 100))))
colnames(y) <- c("very small", "small", "medium", "large")

哪个给出以下输出:

       very small       small     medium    large
 [1,] 0.596546432 0.115485521 0.09401027 99.19396
 [2,] 0.118521670 0.694866442 0.16535686 99.02126
 [3,] 0.421295392 0.002795051 0.03821284 99.53770
 [4,] 0.202813028 0.269167296 0.21997193 99.30805
 [5,] 0.008274178 0.222862752 0.56131816 99.20754
 [6,] 0.031685216 0.303549471 0.04934352 99.61542
 [7,] 0.319768410 0.022998874 0.25970746 99.39753
 [8,] 0.087382152 0.609509549 0.09651936 99.20659
 [9,] 0.773647428 0.086955708 0.02798129 99.11142
[10,] 0.331411455 0.368387836 0.13918296 99.16102
[11,] 0.131787972 0.463235363 0.22744175 99.17753
[12,] 0.293173234 0.345394548 0.23810145 99.12333
[13,] 0.153707973 0.074851698 0.44902831 99.32241
[14,] 0.153896874 0.017483677 0.05351859 99.77510
[15,] 0.182090415 0.502133689 0.29637249 99.01940
[16,] 0.413675049 0.082363683 0.26351923 99.24044
[17,] 0.006476240 0.001355163 0.39702371 99.59514
[18,] 0.558440146 0.177935100 0.13974163 99.12388
[19,] 0.066856618 0.095454829 0.22950901 99.60818
[20,] 0.050508366 0.202233682 0.41133537 99.33592
[21,] 0.152118187 0.373801691 0.01678485 99.45730
[22,] 0.545999638 0.070524028 0.03855460 99.34492
[23,] 0.216591213 0.171351144 0.41215730 99.19990
[24,] 0.214317867 0.388471707 0.24408460 99.15313
[25,] 0.134022010 0.380754224 0.29407828 99.19115
[26,] 0.071872900 0.127088718 0.39654603 99.40449
[27,] 0.105251258 0.554369684 0.23365911 99.10672
[28,] 0.233648165 0.224023261 0.19177330 99.35056
[29,] 0.649248827 0.139018328 0.14818464 99.06355
[30,] 0.090693591 0.034686614 0.10709328 99.76753

在这种情况下,

names尝试为矩阵的每个元素而不是每一列分配名称(就像在数据框中找到的那样)。

认为这是因为names(y)试图为as.list(y)的每个元素分配一个名称,并且数据框已经是一个列表,其中每个元素都是一列,而当您将矩阵强制转换为列表时,行/列结构将丢失。无论哪种方式,矩阵的namescolnames都不一样。