

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to delete a row from an R data frame if any value in the row is greater than n?
To delete a row from an R data frame if any value in the row is greater than n can be done by using the subsetting with single square brackets and negation operator. We will subset the values that are greater than n and then take the negation of the subset as shown in the below given examples.
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,5) x2<-rpois(20,2) x3<-rpois(20,8) df1<-data.frame(x1,x2,x3) df1
Output
The following dataframe is created −
x1 x2 x3 1 4 2 12 2 4 0 10 3 3 0 10 4 5 3 5 5 6 0 8 6 4 1 7 7 2 1 7 8 4 1 10 9 10 0 10 10 6 4 4 11 3 3 13 12 7 0 8 13 3 1 7 14 4 0 6 15 5 3 5 16 4 0 2 17 3 3 7 18 6 2 7 19 7 3 17 20 2 3 11
To remove rows from df1 if any value in the row is greater than 10, add the following code to the above snippet −
df1[!rowSums(df1>10),]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x1 x2 x3 2 4 0 10 3 3 0 10 4 5 3 5 5 6 0 8 6 4 1 7 7 2 1 7 8 4 1 10 9 10 0 10 10 6 4 4 12 7 0 8 13 3 1 7 14 4 0 6 15 5 3 5 16 4 0 2 17 3 3 7 18 6 2 7
Example 2
Following snippet creates a sample data frame −
y1<-rnorm(20) y2<-rnorm(20) y3<-rnorm(20) df2<-data.frame(y1,y2,y3) df2
Output
The following dataframe is created −
y1 y2 y3 1 0.23815055 0.44797910 -0.009917453 2 -0.82579339 -0.67543673 -0.313231987 3 0.93456545 0.34720074 0.856496009 4 -0.14107504 -0.66473395 -0.102566172 5 -0.31613111 0.79806925 -0.464500620 6 0.13722725 0.42098274 0.800216082 7 -0.46063194 -1.19591354 -0.935939317 8 1.25424818 0.77225841 0.508798438 9 1.18270201 0.40060187 0.718047210 10 1.11736153 0.74206460 1.529896849 11 -0.96901766 1.72965814 -1.138898309 12 -0.01484298 0.62858032 1.805046282 13 -0.01423460 0.80616290 -0.290950215 14 -0.80448205 -0.78334124 -0.589883476 15 -0.04113097 -0.22948217 0.689388447 16 -0.66901901 0.85912428 1.078791419 17 -0.07033102 -0.05181978 0.707799373 18 0.21166522 0.73385673 1.254184717 19 0.03052462 0.49958642 0.066300327 20 0.60012460 0.91094585 0.881134455
To remove rows from df2 if any value in the row is greater than 0.5, add the following code to the above snippet −
df2[!rowSums(df2>0.5),]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
y1 y2 y3 1 0.23815055 0.4479791 -0.009917453 2 -0.82579339 -0.6754367 -0.313231987 4 -0.14107504 -0.6647339 -0.102566172 7 -0.46063194 -1.1959135 -0.935939317 14 -0.80448205 -0.7833412 -0.589883476 19 0.03052462 0.4995864 0.066300327
- Related Questions & Answers
- How to delete a row from an R data frame?
- Find the column name that contains value greater than a desired value in each row of an R data frame.
- How to subset rows of an R data frame if any columns have values greater than a certain value?
- Check if any value in an R vector is greater than or less than a certain value.
- How to subset nth row from an R data frame?
- How to find the row products for each row in an R data frame?
- How to find the row corresponding to a nearest value in an R data frame?
- Replace all values in an R data frame if they are greater than a certain value.
- How to reorder the row indices in an R data frame?
- How to change the row order in an R data frame?
- How to add a row to a frame from another data frame in R?
- How to create a sample from an R data frame if weights are assigned to the row values?
- How to create a row sum and a row product column in an R data frame?
- How to divide the row values by row mean in R data frame?
- How to divide the data frame row values in R by row median?