How to add a new column to a matrix in R?


A data collection process is one of the initial and very important tasks in a data analysis project and sometimes we miss something. Therefore, we need to collect that data later and add it to the originally collected data. This mistake can be done for matrix data as well, hence we might need to add a new column to original matrix and this can be done by using cbind function.

Example1

Live Demo

> M1<-matrix(1:25,nrow=5)
> M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25

Example

> V1<-26:30
> M1<-cbind(M1,V1)
> M1

Output

V1
[1,] 1 6 11 16 21 26
[2,] 2 7 12 17 22 27
[3,] 3 8 13 18 23 28
[4,] 4 9 14 19 24 29
[5,] 5 10 15 20 25 30

Example2

Live Demo

> M2<-matrix(rpois(81,5),ncol=9)
> M2

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 7 3 3 4 2 3 6 3 6
[2,] 7 5 6 6 4 4 4 5 3
[3,] 5 6 3 9 4 3 4 6 4
[4,] 4 4 3 6 1 3 6 9 4
[5,] 2 4 4 5 9 2 3 4 4
[6,] 6 5 5 3 3 4 3 9 5
[7,] 11 5 5 6 4 5 3 4 4
[8,] 4 3 1 1 9 3 5 4 3
[9,] 14 7 5 1 6 4 4 6 5

Example

> V2<-rpois(9,10)
> M2<-cbind(M2,V2)
> M2

Output

V2
[1,] 7 3 3 4 2 3 6 3 6 14
[2,] 7 5 6 6 4 4 4 5 3 9
[3,] 5 6 3 9 4 3 4 6 4 7
[4,] 4 4 3 6 1 3 6 9 4 10
[5,] 2 4 4 5 9 2 3 4 4 6
[6,] 6 5 5 3 3 4 3 9 5 11
[7,] 11 5 5 6 4 5 3 4 4 10
[8,] 4 3 1 1 9 3 5 4 3 12
[9,] 14 7 5 1 6 4 4 6 5 13

Example3

Live Demo

> M3<-matrix(rnorm(25,2,0.36),nrow=5)
> M3

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 1.696484 1.269688 1.607353 1.913940 2.463333
[2,] 2.485409 2.107897 1.989745 2.072183 1.120741
[3,] 2.202257 1.715435 2.027664 1.230764 2.435480
[4,] 1.845053 2.132015 1.845209 1.480105 1.870550
[5,] 2.286963 1.776010 1.691820 1.634944 2.091766

Example

> V3<-rnorm(5,5,1)
> M3<-cbind(M3,V3)
> M3

Output

V3
[1,] 1.696484 1.269688 1.607353 1.913940 2.463333 4.961799
[2,] 2.485409 2.107897 1.989745 2.072183 1.120741 5.243562
[3,] 2.202257 1.715435 2.027664 1.230764 2.435480 4.393539
[4,] 1.845053 2.132015 1.845209 1.480105 1.870550 6.008758
[5,] 2.286963 1.776010 1.691820 1.634944 2.091766 5.988232

Example4

Live Demo

> M4<-matrix(round(runif(64,2,5),0),ncol=8)
> M4

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3 5 5 3 4 3 4 3
[2,] 3 3 4 3 3 3 4 3
[3,] 3 4 3 4 4 4 4 2
[4,] 3 2 3 2 5 2 5 4
[5,] 4 4 2 5 3 3 2 3
[6,] 3 4 3 3 4 2 4 5
[7,] 4 3 3 3 4 4 3 2
[8,] 4 3 4 3 4 4 2 5

Example

> V4<-round(runif(8,1,5),0)
> M4<-cbind(M4,V4)
> M4

Output

V4
[1,] 3 5 5 3 4 3 4 3 5
[2,] 3 3 4 3 3 3 4 3 5
[3,] 3 4 3 4 4 4 4 2 4
[4,] 3 2 3 2 5 2 5 4 2
[5,] 4 4 2 5 3 3 2 3 5
[6,] 3 4 3 3 4 2 4 5 4
[7,] 4 3 3 3 4 4 3 2 2
[8,] 4 3 4 3 4 4 2 5 3

Example5

Live Demo

> M5<-matrix(round(rexp(36,1.22),2),ncol=6)
> M5

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.55 0.02 1.27 0.31 0.10 0.08
[2,] 1.13 0.26 0.46 0.06 0.90 0.43
[3,] 0.14 1.26 0.89 0.12 0.16 0.63
[4,] 0.33 0.79 1.03 2.72 0.17 0.49
[5,] 0.17 4.59 0.04 0.26 0.18 1.52
[6,] 0.46 1.05 0.10 0.96 0.64 0.31

Example

> V5<-round(rexp(6,2.1),2)
> M5<-cbind(M5,V5)
> M5

Output

V5
[1,] 1.55 0.02 1.27 0.31 0.10 0.08 0.07
[2,] 1.13 0.26 0.46 0.06 0.90 0.43 0.00
[3,] 0.14 1.26 0.89 0.12 0.16 0.63 0.40
[4,] 0.33 0.79 1.03 2.72 0.17 0.49 0.23
[5,] 0.17 4.59 0.04 0.26 0.18 1.52 0.32
[6,] 0.46 1.05 0.10 0.96 0.64 0.31 0.64

Updated on: 19-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements