 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
How to delete a row from an R data frame?
While doing the analysis, we might come across with data that is not required and we want to delete it. This data can be a whole row or multiple rows. For example, if a row contains values greater than, less than or equal to a certain threshold then it might not be needed, therefore we can delete it. In R, we achieve this with the help of subsetting through single square brackets.
Example
Consider the below data frame −
> set.seed(99) > x1<-rnorm(20) > x2<-rnorm(20,0.1) > x3<-rnorm(20,0.2) > x4<-rnorm(20,0.5) > x5<-rnorm(20,1) > df<-data.frame(x1,x2,x3,x4,x5) > df x1 x2 x3 x4 x5 1 0.2139625022 1.19892152 0.33297863 0.33708211 1.03661152 2 0.4796581346 0.85251346 -1.47926432 0.38578484 1.28852606 3 0.0878287050 0.04058331 -0.07847958 0.05534064 -0.10597134 4 0.4438585075 -0.24456879 -1.35241100 0.75695917 1.89223849 5 -0.3628379205 0.32266830 -1.17969925 -0.60013713 2.18146915 6 0.1226740295 0.65178634 -1.15705659 -0.83657589 1.35116793 7 -0.8638451881 0.78364282 -0.72113718 0.70489861 1.06300672 8 0.4896242667 -0.44587940 -0.66681774 0.53528735 2.39426172 9 -0.3641169125 -1.26743616 1.85664439 0.06108749 0.98749208 10 -1.2942420067 1.50005184 0.04492028 0.90040586 1.67807643 11 -0.7457690454 1.47305395 -1.37655243 1.08517131 0.94385342 12 0.9215503620 0.55025656 0.82408260 0.98212854 1.13599383 13 0.7500543504 -0.04629386 0.53022068 -0.30483385 2.86457602 14 -2.5085540159 0.22809724 -0.19812226 0.80307719 2.14870835 15 -3.0409340953 -2.19472095 -0.88139693 -0.32617573 0.06001394 16 0.0002658005 -1.26656892 0.12307794 0.64142892 0.93811373 17 -0.3940189942 -0.09747955 -0.32553662 1.24035721 0.62390950 18 -1.7450276608 0.16808578 0.59128965 1.88504655 1.20968885 19 0.4986314508 0.19050341 -0.48045326 -0.13357748 1.70545858 20 0.2709537888 0.42275997 -0.54869693 0.73858864 1.65208847
Suppose we want to delete row 1 then we can do it as follows −
> df = df[-1,] > df x1 x2 x3 x4 x5 2 0.4796581346 0.85251346 -1.47926432 0.38578484 1.28852606 3 0.0878287050 0.04058331 -0.07847958 0.05534064 -0.10597134 4 0.4438585075 -0.24456879 -1.35241100 0.75695917 1.89223849 5 -0.3628379205 0.32266830 -1.17969925 -0.60013713 2.18146915 6 0.1226740295 0.65178634 -1.15705659 -0.83657589 1.35116793 7 -0.8638451881 0.78364282 -0.72113718 0.70489861 1.06300672 8 0.4896242667 -0.44587940 -0.66681774 0.53528735 2.39426172 9 - 0.3641169125 -1.26743616 1.85664439 0.06108749 0.98749208 10 -1.2942420067 1.50005184 0.04492028 0.90040586 1.67807643 11 -0.7457690454 1.47305395 - 1.37655243 1.08517131 0.94385342 12 0.9215503620 0.55025656 0.82408260 0.98212854 1.13599383 13 0.7500543504 -0.04629386 0.53022068 -0.30483385 2.86457602 14 -2.5085540159 0.22809724 -0.19812226 0.80307719 2.14870835 15 -3.0409340953 -2.19472095 -0.88139693 -0.32617573 0.06001394 16 0.0002658005 -1.26656892 0.12307794 0.64142892 0.93811373 17 -0.3940189942 -0.09747955 -0.32553662 1.24035721 0.62390950 18 -1.7450276608 0.16808578 0.59128965 1.88504655 1.20968885 19 0.4986314508 0.19050341 -0.48045326 -0.13357748 1.70545858 20 0.2709537888 0.42275997 -0.54869693 0.73858864 1.65208847
Consecutive rows can be deleted in the following way −
> df = df[-c(1:2),] > df x1 x2 x3 x4 x5 4 0.4438585075 -0.24456879 -1.35241100 0.75695917 1.89223849 5 -0.3628379205 0.32266830 -1.17969925 -0.60013713 2.18146915 6 0.1226740295 0.65178634 -1.15705659 -0.83657589 1.35116793 7 -0.8638451881 0.78364282 -0.72113718 0.70489861 1.06300672 8 0.4896242667 -0.44587940 -0.66681774 0.53528735 2.39426172 9 -0.3641169125 -1.26743616 1.85664439 0.06108749 0.98749208 10 -1.2942420067 1.50005184 0.04492028 0.90040586 1.67807643 11 -0.7457690454 1.47305395 -1.37655243 1.08517131 0.94385342 12 0.9215503620 0.55025656 0.82408260 0.98212854 1.13599383 13 0.7500543504 -0.04629386 0.53022068 -0.30483385 2.86457602 14 -2.5085540159 0.22809724 -0.19812226 0.80307719 2.14870835 15 -3.0409340953 -2.19472095 -0.88139693 -0.32617573 0.06001394 16 0.0002658005 -1.26656892 0.12307794 0.64142892 0.93811373 17 -0.3940189942 -0.09747955 -0.32553662 1.24035721 0.62390950 18 -1.7450276608 0.16808578 0.59128965 1.88504655 1.20968885 19 0.4986314508 0.19050341 -0.48045326 -0.13357748 1.70545858 20 0.2709537888 0.42275997 -0.54869693 0.73858864 1.65208847
Now we might to want remove row 1 and row 3, therefore we will be removing 4 and 6 from df and it can be done as shown below −
> df = df[-c(1,3),] > df x1 x2 x3 x4 x5 5 -0.3628379205 0.32266830 -1.17969925 -0.60013713 2.18146915 7 -0.8638451881 0.78364282 -0.72113718 0.70489861 1.06300672 8 0.4896242667 -0.44587940 -0.66681774 0.53528735 2.39426172 9 -0.3641169125 -1.26743616 1.85664439 0.06108749 0.98749208 10 -1.2942420067 1.50005184 0.04492028 0.90040586 1.67807643 11 -0.7457690454 1.47305395 -1.37655243 1.08517131 0.94385342 12 0.9215503620 0.55025656 0.82408260 0.98212854 1.13599383 13 0.7500543504 -0.04629386 0.53022068 -0.30483385 2.86457602 14 -2.5085540159 0.22809724 -0.19812226 0.80307719 2.14870835 15 -3.0409340953 -2.19472095 -0.88139693 -0.32617573 0.06001394 16 0.0002658005 -1.26656892 0.12307794 0.64142892 0.93811373 17 -0.3940189942 -0.09747955 -0.32553662 1.24035721 0.62390950 18 -1.7450276608 0.16808578 0.59128965 1.88504655 1.20968885 19 0.4986314508 0.19050341 -0.48045326 -0.13357748 1.70545858 20 0.2709537888 0.42275997 -0.54869693 0.73858864 1.65208847
Advertisements
 