如何使用列名称从 R 中的矩阵中移除一列?


要使用列名从 R 中的矩阵中移除一列,我们可以按照以下步骤操作 -

  • 首先,创建一个矩阵

  • 然后,为矩阵的列添加名称。

  • 此后,通过取消选择所需列并使用单方括号进行子集化,对矩阵进行子集化。

示例

创建矩阵

让我们创建一个如以下所示的矩阵 -

M<-matrix(rnorm(75),ncol=3)
M

输出

在执行时,上述脚本将生成以下输出(此输出将在您的系统上因随机化而有所不同)-

           [,1]      [,2]        [,3]
[1,]   0.34927825  0.36395550 -1.4312211
[2,]   0.78629719 -0.45147440 -0.1596172
[3,]   0.88492110 -0.16546013 -1.7026552
[4,]  -2.28671282 -1.84818688  1.3984903
[5,]   0.02194021 -0.54682956 -2.5447857
[6,]   1.51718583 -0.34319221 -1.7286530
[7,]  -0.04924026 -0.88751938 -0.1783474
[8,]  -0.07423773 -0.27037703  0.3952588
[9,]   1.14769892 -1.71712705  0.1677618
[10,]  0.71949312 -0.73954427  0.4089844
[11,] -0.33954351 -0.01039998  0.6405262
[12,]  0.11824754  2.27291701  0.2234398
[13,]  0.37339259 -0.50529084  1.0764492
[14,]  0.05557717 -0.61960682  0.4721451
[15,]  1.87575124  1.66763649  0.8596300
[16,] -0.57575272 -0.44936940  0.6154190
[17,]  0.85030730 -1.21989570  1.5136665
[18,]  1.19599580  0.33216810 -0.3205572
[19,]  0.64010472 -0.26696943  1.3040537
[20,]  0.74811799  0.65594635 -1.2104943
[21,] -0.27932027 -1.41589620 -0.1530550
[22,] -0.37549109 -1.08739383  0.7536317
[23,]  0.07836645  1.14317049 -1.1269287
[24,]  0.44175649  0.21147460 -1.3777469
[25,]  1.11577514  0.28621068  0.0819287

添加列名

使用 colnames 函数将列名添加到矩阵 M -

M<-matrix(rnorm(75),ncol=3)
colnames(M)<-c("First","Second","Third")
M

输出

          First      Second      Third
[1,]  -0.63798951 -0.08312581 -0.29548313
[2,]   0.81035121  0.32946453  0.26934501
[3,]   1.17310898 -0.16824116  1.44146054
[4,]  -1.46085561 -1.01480047  0.48119221
[5,]  -0.60117020 -0.71335771  0.56736308
[6,]   1.39032577  0.45488133  1.12518802
[7,]   1.24992297 -0.79274785  0.88435795
[8,]  -0.46766814 -0.61113426  0.87081178
[9,]  -0.50900441  0.32142161  0.06270336
[10,]  0.53407605  0.75097220  0.62138186
[11,] -0.31153258 -0.19474785  1.31048238
[12,] -0.90701432  0.25399274 -0.51568591
[13,]  0.48485802 -0.19454370 -0.84981770
[14,]  0.22094696 -1.04421982 -1.08446966
[15,]  0.28317116 -0.07426917  0.41447679
[16,]  0.60986979  2.20385278  1.02703888
[17,] -1.03122232 -0.26323809 -0.22929783
[18,] -1.39070018  3.28175028  0.31980456
[19,]  0.20176785  1.64951864 -0.51179577
[20,] -0.46897146  1.16688302 -1.76417685
[21,]  0.43936821 -0.70327534  0.05285094
[22,] -0.69668353  0.65657864  0.04483215
[23,]  0.05226474  0.14180989  1.31808786
[24,]  0.16654568  0.74867083  1.16400816
[25,] -1.16417323 -0.42192382  0.87543185

使用列名从矩阵中移除列

通过取消选择第二列并使用单方括号进行子集化,对矩阵进行子集化,如下所示 -

M<-matrix(rnorm(75),ncol=3)
colnames(M)<-c("First","Second","Third")
M<M[,colnames(M)!="Second"]
M

输出

         First      Third
[1,]   0.34927825 -1.4312211
[2,]   0.78629719 -0.1596172
[3,]   0.88492110 -1.7026552
[4,]  -2.28671282  1.3984903
[5,]   0.02194021 -2.5447857
[6,]   1.51718583 -1.7286530
[7,]  -0.04924026 -0.1783474
[8,]  -0.07423773  0.3952588
[9,]   1.14769892  0.1677618
[10,]  0.71949312  0.4089844
[11,] -0.33954351  0.6405262
[12,]  0.11824754  0.2234398
[13,]  0.37339259  1.0764492
[14,]  0.05557717  0.4721451
[15,]  1.87575124  0.8596300
[16,] -0.57575272  0.6154190
[17,]  0.85030730  1.5136665
[18,]  1.19599580 -0.3205572
[19,]  0.64010472  1.3040537
[20,]  0.74811799 -1.2104943
[21,] -0.27932027 -0.1530550
[22,] -0.37549109  0.7536317
[23,]  0.07836645 -1.1269287
[24,]  0.44175649 -1.3777469
[25,]  1.11577514  0.0819287

更新于: 16-11-2021

5K+ 浏览量

开启你的 职业生涯

完成课程获得认证

开始
广告