How to remove a column from matrix in R by using its name?


To remove a column from matrix in R by using its name, we can follow the below steps −

  • First of all, create a matrix

  • Then, add names to columns of the matrix.

  • After that, subset the matrix by deselecting the desired column with negation and single square brackets for subsetting.

Example

Create the matrix

Let’s create a matrix as shown below −

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

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

           [,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

Add the column names

Using colnames function to add the column names to matrix M −

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

Output

          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

Remove the column from matrix using column name

Subset the matrix by deselecting the Second column with negation and single square brackets as shown below −

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

Output

         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

Updated on: 16-Nov-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements