Method 3 : Remove Rows by subset() function
The subset() function creates a subset of a given dataframe based on certain conditions. This helps us to remove or select the rows of data with single or multiple conditional statements. The subset() function is an inbuilt function of the R Language and does not need any third-party package to be imported.
Syntax:
subset( df, Conditional-statement )
where,
- df: determines the dataframe to be used.
- conditional-statement: determines the condition for filtering data.
Example:
In this example. all data points where the x variable is less than 19 and the y variable is greater than 50 are removed using the subset function.
R
# create sample data sample_data <- data.frame ( x = rnorm (10,20), y= rnorm (10,50) ) # print data print ( "Sample Data:" ) sample_data # filter data new_data = subset (sample_data, sample_data$x > 19 & sample_data$y < 49 ) # print data print ( "Filtered Data:" ) new_data |
Output:
Sample Data: x y 1 20.38324 51.02714 2 20.36595 50.64125 3 20.44204 52.28653 4 20.34413 50.08981 5 20.51478 49.53950 6 20.35667 48.88035 7 19.89415 49.78139 8 21.61003 49.43653 9 20.66579 49.14877 10 20.70246 50.06486 Filtered Data: x y 6 20.35667 48.88035
How to Conditionally Remove Rows in R DataFrame?
In this article, we will discuss how to conditionally remove rows from a dataframe in the R Programming Language. We need to remove some rows of data from the dataframe conditionally to prepare the data. For that, we use logical conditions on the basis of which data that doesn’t follow the condition is removed.