How to Compare Two Columns in R DataFrame? Last Updated : 19 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to compare two columns in the dataframe through R Programming Language. We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly. Syntax: ifelse(df$column1 > df$column2, statement,............) where, df is the input dataframecolumn are the columns in the given dataframe Example: Let's create the dataframe with two columns R # dataframe data = data.frame(column1=c(90, 76, 89), column2=c(89, 79, 100)) # display data Output: Example 1: Here , we are going to check if column1 value is greater and if greater then add a new column named results and assign with Column1. If column2 value is greater then add a new column named results and assign it with Column2. otherwise None R # dataframe data = data.frame(column1=c(90, 76, 89), column2=c(90, 79, 100)) # check if column1 value is greater - if greater # then add a new column named results and assign # with Column1 # if column2 value is greater - if greater then # add a new column named results and assign with # Column2 otherwise None data$results = ifelse(data$column1 > data$column2, 'Column1', ifelse(data$column1 < data$column2, 'Column2', 'None')) # display data Output: Example 2: Here , we are going to check if the column1 value is greater then add a new column named results and assign with Column1. if column2 value is greater then add a new column named results and assign it with Column2. otherwise None R # dataframe data = data.frame(column1=c(70, 76, 89), column2=c(90, 79, 100)) # check if column1 value is greater - if # greater then add a new column named results # and assign with Column1 # if column2 value is greater - if greater then # add a new column named results and assign with # Column2 otherwise None data$results = ifelse(data$column1 > data$column2, 'Column1', ifelse(data$column1 < data$column2, 'Column2', 'None')) # display data Output: Comment More infoAdvertise with us Next Article How to Sort a DataFrame in R ? O ojaswilavu8128 Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs Similar Reads How to add column to dataframe in R ? In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec 2 min read Combine two DataFrames in R with different columns In this article, we will discuss how to combine two dataframes with different columns in R Programming Language. Method 1 : Using plyr package The "plyr" package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the 5 min read How to add dataframe to dataframe in R ? In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language. Method 1: Using rbind() method The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different num 4 min read How to find common rows and columns between two dataframe in R? Two data frames can have similar rows, and they can be determined. In this article, we will find the common rows and common columns between two data frames, in the R programming language. Approach Create a first data frameCreate a second data frameCompare using required functionsCopy same rows to an 2 min read How to Sort a DataFrame in R ? In this article, we will discuss how to sort the dataframe in R Programming Language. In R DataFrame is a two-dimensional tabular data structure that consists of rows and columns. Sorting a DataFrame allows us to reorder the rows based on the values in one or more columns. This can be useful for var 5 min read How to Select Specific Columns in R dataframe? In this article, we will discuss how to select specific columns from a data frame in the R Programming Language. Selecting specific Columns Using Base R by column nameIn this approach to select a specific column, the user needs to write the name of the column name in the square bracket with the name 7 min read Like