Create a matrix for odd number of elements by filling the last element with NA in R.



We can find the total number of elements for a matrix with the help of prod and dim function as shown in the below Examples. To create a matrix for odd number of elements by filling the last element with NA, we can use byrow argument.

For Example, if we have a vector called V that contains 19 elements then we can create a matrix called M having 20 elements NA as the last element by using the below command −

M<-matrix(V,ncol=2,byrow=TRUE)

Example 1

Consider the vector given below −

x1<-rpois(39,5)
length(x1)<-prod(dim(matrix(x1,ncol=2)))

Warning message

In matrix(x1, ncol = 2) : data length [39] is not a sub-multiple or multiple of the number of rows [20]

To create matrix of x1 on the above created matrix, add the following code to the above snippet −

M1<-matrix(x1,ncol=2,byrow=TRUE)
M1

The following matrix is created −

    [,1] [,2]
[1,]  2   5
[2,] 10   3
[3,]  3   5
[4,]  5   5
[5,]  5   5
[6,]  4   3
[7,]  2   3
[8,]  5   4
[9,]  6   6
[10,] 5   4
[11,] 6   3
[12,] 3   6
[13,] 6   4
[14,] 4   7
[15,] 7   5
[16,] 1   4
[17,] 3   4
[18,] 6   5
[19,] 4   6
[20,] 2  NA

Example 2

Consider the vector given below −

x2<-rnorm(39)
length(x2)<-prod(dim(matrix(x2,ncol=2)))

Warning message

In matrix(x2, ncol = 2) : data length [39] is not a sub-multiple or multiple of the number of rows [20]

To create matrix of x2 on the above created matrix, add the following code to the above snippet −

M2<-matrix(x2,ncol=2,byrow=TRUE)
M2

The following matrix is created −

[,1] [,2]
[1,]   0.66716447 -0.122225066
[2,]  -0.07647741  0.028916676
[3,]  -0.39709805  1.641409012
[4,]  -0.24942074 -0.158497321
[5,]   0.02107895  0.003907016
[6,]  -2.05324534  0.979486325
[7,]  -0.71567237 -0.499532064
[8,]  -0.28274355 -1.111749126
[9,]  -0.24421395 -0.615584797
[10,] -1.40823602  0.664978538
[11,] -1.13424418  1.906882422
[12,] -1.50972716  0.587050358
[13,] -0.04610703  0.402558630
[14,] -0.85797812 -0.948390838
[15,] -0.11944543 -1.415887582
[16,] -0.76236171 -0.500234600
[17,] -1.40270152  0.031947267
[18,] -1.85293591 -0.383315786
[19,] -1.35704286  0.023015415
[20,]  1.06800735   NA

Example 3

Consider the vector given below −

x3<-sample(0:9,79,replace=TRUE)
length(x3)<-prod(dim(matrix(x3,ncol=4)))

Warning message

In matrix(x3, ncol = 4) : data length [79] is not a sub-multiple or multiple of the number of rows [20]

To create matrix of x3 on the above created matrix, add the following code to the above snippet −

M3<-matrix(x3,ncol=4,byrow=TRUE)
M3

The following matrix is created −

    [,1] [,2] [,3] [,4]
[1,]   1   0   2    4
[2,]   1   5   6    7
[3,]   6   9   0    3
[4,]   3   2   2    7
[5,]   9   5   2    2
[6,]   6   5   1    2
[7,]   8   1   3    1
[8,]   3   6   4    7
[9,]   0   1   2    1
[10,]  8   0   6    0
[11,]  1   8   6    1
[12,]  2   5   0    6
[13,]  0   3   1    5
[14,]  4   3   7    7
[15,]  9   7   4    7
[16,]  2   4   6    0
[17,]  9   8   4    3
[18,]  9   9   0    6
[19,]  6   3   0    4
[20,]  2   4   1   NA

Advertisements