How to create a matrix without column and row indices in R?

R ProgrammingServer Side ProgrammingProgramming

To create a matrix without column and row indices, we first need to create the matrix and then prmatrix function can be used to convert that matrix without column and row indices but we would need to provide the number of rows inside the function. For example, if we have a matrix M that contains 5 rows and 5 columns then it can be converted to a matrix without column and row indices using prmatrix(M,rowlab=rep("",5),collab=rep("",5)).

Example

 Live Demo

M1<-matrix(rnorm(25),ncol=5)
M1

Output

         [,1]       [,2]       [,3]    [,4]       [,5]
[1,] -0.3524287 0.4753824 -0.7817841 0.4452398 1.5483299
[2,] 0.7659213 0.8160877 -0.9124624 0.3859241 -0.3645244
[3,] -0.7511866 0.4664604 -2.3584599 0.2926155 0.0354159
[4,] -0.2477745 -0.7132980 -0.6503972 -0.3519619 -0.7805707
[5,] 0.5781549 -0.1417311 -0.4956891 1.1003766 0.2343090

Example

prmatrix(M1,rowlab=rep("",5),collab=rep("",5))

Output

-0.3524287 0.4753824 -0.7817841 0.4452398 1.5483299
0.7659213 0.8160877 -0.9124624 0.3859241 -0.3645244
-0.7511866 0.4664604 -2.3584599 0.2926155 0.0354159
-0.2477745 -0.7132980 -0.6503972 -0.3519619 -0.7805707
0.5781549 -0.1417311 -0.4956891 1.1003766 0.2343090

Example

 Live Demo

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

Output

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

Example

prmatrix(M2,rowlab=rep("",9),collab=rep("",9))

Output

3 1 3 2 0 2 0 2 4
3 0 1 4 0 3 2 1 2
4 3 2 1 4 3 3 3 4
0 2 3 3 1 2 2 3 4
1 1 2 3 2 1 2 4 1
2 3 0 1 1 2 4 3 2
6 3 0 1 2 0 1 1 1
6 3 3 2 5 1 0 0 0
4 1 2 3 2 2 0 1 3

Example

 Live Demo

M3<-matrix(rexp(36,1.27),ncol=2)
M3

Output

      [,1]          [,2]
[1,] 1.90172301 0.17485982
[2,] 0.72992558 1.22893098
[3,] 0.34723400 0.03796494
[4,] 0.03941449 1.95001158
[5,] 1.04126572 1.14392629
[6,] 0.25201154 0.57012496
[7,] 1.30187720 0.96230866
[8,] 1.13920057 1.40368354
[9,] 1.41180402 0.12139199
[10,] 0.53137688 0.01768795
[11,] 0.05370309 1.69081321
[12,] 1.13580395 0.11810967
[13,] 0.56185639 1.44204980
[14,] 1.51025894 0.23696430
[15,] 1.26819806 0.07203026
[16,] 0.30501141 0.14618651
[17,] 0.93060957 0.07317170
[18,] 0.92067027 0.08642117

Example

prmatrix(M3,rowlab=rep("",18),collab=rep("",18))

Output

1.90172301 0.17485982
0.72992558 1.22893098
0.34723400 0.03796494
0.03941449 1.95001158
1.04126572 1.14392629
0.25201154 0.57012496
1.30187720 0.96230866
1.13920057 1.40368354
1.41180402 0.12139199
0.53137688 0.01768795
0.05370309 1.69081321
1.13580395 0.11810967
0.56185639 1.44204980
1.51025894 0.23696430
1.26819806 0.07203026
0.30501141 0.14618651
0.93060957 0.07317170
0.92067027 0.08642117

Example

 Live Demo

M4<-matrix(runif(40,2,5),nrow=20)
M4

Output

      [,1]       [,2]
[1,] 2.884068 2.732077
[2,] 3.694684 2.517772
[3,] 4.165912 3.924986
[4,] 3.752682 2.414989
[5,] 3.199349 4.666055
[6,] 2.365035 3.800645
[7,] 3.121915 2.395140
[8,] 2.147690 2.463121
[9,] 4.697216 4.965934
[10,] 2.094644 4.681206
[11,] 4.071888 2.360031
[12,] 3.047279 2.485864
[13,] 2.921051 4.556291
[14,] 4.655183 4.367924
[15,] 4.727901 3.647091
[16,] 4.807526 4.086226
[17,] 2.193533 2.246970
[18,] 3.198273 3.197613
[19,] 2.371362 3.260453
[20,] 4.070255 3.981377

Example

prmatrix(M4,rowlab=rep("",20),collab=rep("",20))

Output

2.884068 2.732077
3.694684 2.517772
4.165912 3.924986
3.752682 2.414989
3.199349 4.666055
2.365035 3.800645
3.121915 2.395140
2.147690 2.463121
4.697216 4.965934
2.094644 4.681206
4.071888 2.360031
3.047279 2.485864
2.921051 4.556291
4.655183 4.367924
4.727901 3.647091
4.807526 4.086226
2.193533 2.246970
3.198273 3.197613
2.371362 3.260453
4.070255 3.981377
raja
Published on 07-Dec-2020 05:06:09
Advertisements