How to find difference between two columns in R?

Generally, the difference between two columns can be calculated from a dataframe that contains some numeric data. In this article, we will discuss how the difference between columns can be calculated in the R programming language.

Create a dataframe and the columns should be of numeric or integer data type so that we can find the difference between them.
Extract required data from columns using the $ operator into separate variables. For example, we have two columns then extract individual columns into separate variables.
Then perform the minus operation for the difference between those columns.
Finally, print the result.


# creating a dataframe
# Extracting column 1
# Extracting column 2
# printing dataframe
# printing difference among
# two columns


  num num1
1   1   5 
2   2   4
3   3   3 
4   4   2 
> print (b-a) 
[1] 4  2 0  -2