How to remove only first row from a data.table object in R?


To remove only first row from a data.table object we can follow the below steps −

  • First of all, creating the data.table object.
  • Subsetting the data frame with single square brackets by negation of 1

Example1

Create the data frame

Let's create a data frame as shown below −

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

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

        x1         x2
1:  -0.98669320  -0.44240685
2:  -0.09147729  1.72503085
3:  -1.12646564  0.37485654
4:  -0.30616769  0.86033149
5:   1.96730090  1.01282182
6:   0.70226615  3.17735110
7:   0.67209073  0.04031375
8:   0.11480893 -0.09316635
9:  -0.63096090  0.03580160
10:  0.59003315 -0.60911498
11: -2.27800542  1.59919222
12:  1.19723865  0.83675087
13:  0.64070195  0.46171996
14:  1.31641254  -0.36089120
15: -1.02857852  0.62663990
16:  0.17363001  1.84694082
17: -0.49364585 0.98331604
18: -0.31570860  -0.09551791
19:  0.41419886  1.28203283
20: -0.51659304  1.03185536

Removing first row from data.table object

Use single square brackets for subsetting DT1 and removing first row in DT1 −

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

Output

        x1         x2
1: -0.09147729   1.72503085
2: -1.12646564   0.37485654
3: -0.30616769   0.86033149
4:  1.96730090   1.01282182
5:  0.70226615   3.17735110
6:  0.67209073   0.04031375
7:  0.11480893 -0.09316635
8: -0.63096090  0.03580160
9:  0.59003315 -0.60911498
10: -2.27800542 1.59919222
11: 1.19723865   0.83675087
12: 0.64070195   0.46171996
13:  1.31641254 -0.36089120
14: -1.02857852 0.62663990
15:  0.17363001  1.84694082
16: -0.49364585  0.98331604
17: -0.31570860 -0.09551791
18:  0.41419886 1.28203283
19: -0.51659304  1.03185536

Example2

Creating the data.table object

Load data.table package and create a data.table object as shown below −

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

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

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

Removing first row from data.table object

Use single square brackets for subsetting DT2 and removing first row in DT2 −

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

Output

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

Updated on: 13-Aug-2021

571 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements