
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Last Few Rows from an R Data Frame
An R data frame can contain a very large number of rows and we might want to get rid of some rows if they’re not supposed to be helpful in our data analysis. Therefore, we can remove these rows prior to starting the analysis process. We can say that this removal of some rows is a part of data cleaning and obviously data cleaning helps us creating a smooth data set for analysis. In R, we can simply use head function to remove last few rows from an R data frame, also we can store them as a new data frame if we want to but I will just show you how to remove the rows and you can assign a object name to the new df if you feel so.
Example
Consider the below data frame −
set.seed(111) x1<-rnorm(20,2) x2<-LETTERS[1:20] x3<-rep(c("L1","L2","L3","L4"),times=5) x4<-rpois(20,5) x5<-sample(1:1000,20) df<-data.frame(x1,x2,x3,x4,x5) df
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252 11 1.8263259 K L3 6 170 12 1.5934012 L L4 5 592 13 3.8456363 M L1 1 42 14 2.3940541 N L2 4 423 15 2.7975285 O L3 12 388 16 0.4333346 P L4 5 308 17 1.9141490 Q L1 5 113 18 1.6408605 R L2 5 998 19 0.8063910 S L3 2 310 20 2.3641867 T L4 7 687
Example
head(df,-5)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252 11 1.8263259 K L3 6 170 12 1.5934012 L L4 5 592 13 3.8456363 M L1 1 42 14 2.3940541 N L2 4 423 15 2.7975285 O L3 12 388
Example
head(df,-2)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252 11 1.8263259 K L3 6 170 12 1.5934012 L L4 5 592 13 3.8456363 M L1 1 42 14 2.3940541 N L2 4 423 15 2.7975285 O L3 12 388 16 0.4333346 P L4 5 308 17 1.9141490 Q L1 5 113 18 1.6408605 R L2 5 998
Example
head(df,-10)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252
Example
head(df,-8)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252 11 1.8263259 K L3 6 170 12 1.5934012 L L4 5 592
Example
head(df,-15)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855
Example
head(df,-4)
Output
x1 x2 x3 x4 x5 1 2.2352207 A L1 6 61 2 1.6692641 B L2 4 893 3 1.6883762 C L3 6 91 4 -0.3023457 D L4 8 605 5 1.8291240 E L1 5 855 6 2.1402782 F L2 4 864 7 0.5025733 G L3 4 373 8 0.9898116 H L4 7 493 9 1.0515244 I L1 6 769 10 1.5060378 J L2 7 252 11 1.8263259 K L3 6 170 12 1.5934012 L L4 5 592 13 3.8456363 M L1 1 42 14 2.3940541 N L2 4 423 15 2.7975285 O L3 12 388 16 0.4333346 P L4 5 308
Advertisements