How to use Square Brackets In R Language

By using < operator inside the square bracket we can return the required rows.

Syntax:

dataframe[dataframe$column1 < dataframe$column2,]

where,

  • dataframe is the input dataframe
  • column1 is the first column
  • column2 is the second column

Example: R program to select rows only if first column values is less than second column values

R




# create a dataframe with 6 rows and 2 columns
data=data.frame(sub1=c(100,89,90,78,98,93),
                sub2=c(89,91,97,67,100,89))
  
# select rows only if first column values 
# is less than second column values
print(data[data$sub1 < data$sub2,] )
  
# select rows only if second column values 
# is less than first column values
print(data[data$sub2 < data$sub1,] )


Output:

Select Rows if Value in One Column is Smaller Than in Another in R Dataframe

In this article, we will discuss how to select rows if the value in one column is smaller than another in dataframe in R programming language.

Data frame in use:

Similar Reads

Method 1: Using Square Brackets

By using < operator inside the square bracket we can return the required rows....

Method 2 : Using subset() function

...

Method 3 : Using filter() function

This function gets the subset of the data from the dataframe where the condition is specified....