Remove Line Numbers from Data Table Object in R



To remove line numbers from data.table object in R, we can set row.names to FALSE and print the data.table object.

For Example, if we have a data.table object called DT then we can remove line numbers from DT by using the command given below −

print(DT,row.names=FALSE)

Example 1

To load data.table object and create an object use the following snippet to create a data frame −

library(data.table)
x1<-rnorm(20)
x2<-rnorm(20)
DT1<-data.table(x1,x2)
DT1

The following dataframe is created

            x1        x2
1:  -0.02658483  0.169448948
2:  -1.43115388 -0.864877686
3:  -0.50281119 -0.700596497
4:   1.38780360 -1.897366568
5:   0.35763631  0.478451912
6:  -0.35764002  0.214175540
7:  -0.20389025 -0.169978581
8:   1.57441418 -1.869871317
9:  -0.55489836  0.619942102
10: -2.00224453 -0.915234113
11: -0.75488901  0.533122831
12: -0.78726955  0.905360849
13:  0.12579643 -0.938330049
14: -0.63961706 -0.239780606
15: -1.94153891  0.001229949
16: -0.19457437  0.376066187
17:  1.59071927  0.348293746
18:  0.12191501  0.857503511
19: -0.67102720  0.799023371
20: -0.04910786  0.450912525

To remove row numbers from DT1 add the following code to the above snippet −

library(data.table)
x1<-rnorm(20)
x2<-rnorm(20)
DT1<-data.table(x1,x2)
print(DT1,row.names=FALSE)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

       x1        x2
-0.02658483  0.169448948
-1.43115388 -0.864877686
-0.50281119 -0.700596497
 1.38780360 -1.897366568
 0.35763631  0.478451912
-0.35764002  0.214175540
-0.20389025 -0.169978581
 1.57441418 -1.869871317
-0.55489836  0.619942102
-2.00224453 -0.915234113
-0.75488901  0.533122831
-0.78726955  0.905360849
 0.12579643 -0.938330049
-0.63961706 -0.239780606
-1.94153891  0.001229949
-0.19457437  0.376066187
 1.59071927  0.348293746
 0.12191501  0.857503511
-0.67102720  0.799023371
-0.04910786  0.450912525

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,1)
y2<-rpois(20,5)
y3<-rpois(20,2)
DT2<-data.table(y1,y2,y3)
DT2

The following dataframe is created

   y1 y2 y3
1:  1 1  2
2:  1 9  4
3:  2 8  1
4:  3 6  0
5:  1 5  2
6:  1 5  2
7:  0 3  1
8:  1 6  4
9:  0 1  4
10: 0 8  5
11: 1 9  1
12: 0 1  2
13: 0 7  2
14: 1 9  2
15: 1 6  3
16: 0 5  3
17: 0 9  2
18: 0 3  0
19: 1 4  0
20: 1 3  3

To remove row numbers from DT2 on the above created data frame, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,5)
y3<-rpois(20,2)
DT2<-data.table(y1,y2,y3)
print(DT2,row.names=FALSE)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

y1 y2 y3
1  1  2
1  9  4
2  8  1
3  6  0
1  5  2
1  5  2
0  3  1
1  6  4
0  1  4
0  8  5
1  9  1
0  1  2
0  7  2
1  9  2
1  6  3
0  5  3
0  9  2
0  3  0
1  4  0
1  3  3
Updated on: 2021-11-05T06:31:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements