There are multiple ways to filter rows based on condition in R data frame.

The following methods show how you can do it with syntax.

Method 1: Use filter() Function

library(dplyr)

df %>% filter(column > n)

Method 2: Use Logical Indexing

df[df$column > n, ]

Method 3: Use subset() Function

subset(df, column > n)

The following examples show how to filter rows based on condition in a dataframe.

Using filter() Function

Let’s see how we can use filter() function from dplyr package:

library(dplyr)

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Filter rows
filter_data <- df %>% filter(Value > 105)

# Show filter data
print(filter_data)

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 2001-01-19     Machine3   135      91

Here the output shows a rows having Value column greater than 105 and we filterrd out this rows using filter() function.

Using Logical Indexing

Let’s see how we can filter rows using logical operator in R:

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Apply condition to filter data
filter_data <- df[df$Value < 105,]

# Print filtered dataframe 
print(filter_data)

Output:

  Start_date Machine_name Value Reading
2 1998-03-28     Machine2    99      97
4 2003-04-27     Machine4    95      89
5 2004-11-26     Machine5    98      80

In the output it shows a rows having Value column less than 105.

Using subset() Function

You can use subset() function to filter rows of dataframe in R:

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Filter rows using subset()
filter_data <- subset(df, Reading > 90)

# Show filtered data
print(filter_data)

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 1998-03-28     Machine2    99      97
3 2001-01-19     Machine3   135      91

Here the above output it shows a rows having Reading column greater than 90.