How to print matrix without line numbers in R?


To print matrix without line numbers in R, we can follow the below steps −

  • First of all, create a matrix.

  • Then, print the matrix with as.data.frame and row.names argument set to FALSE.

Example1

Create the matrix

Let’s create a matrix as shown below −

 Live Demo

M1<-matrix(rpois(60,5),ncol=3)
M1

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

Output

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

Removing line numbers from the matrix

Using as.data.frame function and print function to print the matrix M1 without line numbers −

 Live Demo

M1<-matrix(rpois(60,5),ncol=3)
print(as.data.frame(M1),row.names=F)

Output

V1  V2 V3
9   4  2
3   4  7
3   3  4
3   9  8
4   3  7
6   8  5
9   5  3
2   1  6
1   8  7
8   3  5
3   6  5
8   4  5
6   6  4
2   3  2
3   5  3
5   11 7
4   4  7
2   4  4
4   7  4
6   5  7

Example 2

Create the matrix

Let’s create a matrix as shown below −

 Live Demo

M2<-matrix(round(rnorm(80),1),ncol=4)
M2

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

Output

   [,1] [,2] [,3] [,4]
[1,] -0.7 0.9 0.3 -0.6
[2,] -1.0 -0.8 0.6 -1.3
[3,] -1.9 -1.2 -1.9 -0.8
[4,] -1.8 0.7 -1.7 -1.0
[5,] -2.1 -0.3 1.6 0.8
[6,] -1.1 -0.9 -1.7 -1.9
[7,] -0.1 1.5 -1.2 0.2
[8,] 0.5 -0.9 0.0 -0.3
[9,] 0.3 2.1 -0.2 1.0
[10,] 1.3 0.2 -0.6 0.5
[11,] -0.5 1.2 -0.8 -0.9
[12,] -1.7 -1.0 0.6 -0.2
[13,] 1.0 0.3 0.8 -0.1
[14,] -0.1 1.9 -2.3 -0.3
[15,] -0.5 -1.3 0.1 -0.7
[16,] 1.3 1.0 0.8 -0.5
[17,] 1.2 -0.4 2.4 0.3
[18,] 0.9 -1.1 0.4 -1.5
[19,] 0.6 -0.7 -2.7 1.1
[20,] -1.4 -0.5 0.5 -0.4

Removing line numbers from the matrix

Using as.data.frame function and print function to print the matrix M1 without line numbers −

 Live Demo

M2<-matrix(rpois(80,1),ncol=4)
print(as.data.frame(M1),row.names=F)

Output

 V1   V2  V3  V4
-0.7 0.9 0.3 -0.6
-1.0 -0.8 0.6 -1.3
-1.9 -1.2 -1.9 -0.8
-1.8 0.7 -1.7 -1.0
-2.1 -0.3 1.6 0.8
-1.1 -0.9 -1.7 -1.9
-0.1 1.5 -1.2 0.2
0.5 -0.9 0.0 -0.3
0.3 2.1 -0.2 1.0
1.3 0.2 -0.6 0.5
-0.5 1.2 -0.8 -0.9
-1.7 -1.0 0.6 -0.2
1.0 0.3 0.8 -0.1
-0.1 1.9 -2.3 -0.3
-0.5 -1.3 0.1 -0.7
1.3 1.0 0.8 -0.5
1.2 -0.4 2.4 0.3
0.9 -1.1 0.4 -1.5
0.6 -0.7 -2.7 1.1
-1.4 -0.5 0.5 -0.4

Updated on: 11-Aug-2021

717 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements