Sum of two columns

The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using “+” operator.

Syntax:

dataframe$column1 + dataframe$column2

where

  • dataframe is the input dataframe
  • column1 is one column in the dataframe
  • column2 is another column in the dataframe

Example: R program to add two columns in dataframe

R




# create a dataframe with subjects marks
data=data.frame(sub1=c(90,89,78,89),
                sub2=c(100,90,86,84),
                sub3=c(89,90,98,99),
                sub4=c(86,78,87,99))
  
# add column1 and column2
print(data$sub1 + data$sub2)
  
# add column1 and column3
print(data$sub1 + data$sub3)
  
# add column1 and column4
print(data$sub1 + data$sub4)
  
# add column2 and column4
print(data$sub2 + data$sub4)


Output:

[1] 190 179 164 173
[1] 179 179 176 188
[1] 176 167 165 188
[1] 186 168 173 183

Sum of Two or Multiple DataFrame Columns in R

In this article, we will discuss how to perform some of two and multiple dataframes columns in R programming language.

Database in use:

Similar Reads

Sum of two columns

The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using “+” operator....

Sum of multiple columns

...