To calculate sum of specific column of dataframe you can use rowSums() function in R.
The following method shows how you can do it with syntax.
Method 1: Use rowSums() Function
rowSums(df[ , c(columns to sum)])
The following example shows how to use rowSums() function in R.
Use rowSums() to Calculate Sum of Specific Columns
Let’s see how we can calculate sum of specific column of data frame:
# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F"),
Pressure=c(78.2, 71.7, 80.21, 83.12, 82.56, 79.50),
Temperature=c(31, 35, 36, 36, 38, 32),
Status=c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE))
# Calculate sum of specific columns
sum<-rowSums(df[ , c("Pressure","Temperature")])
# Print sum
print(sum)
Output:
[1] 109.20 106.70 116.21 119.12 120.56 111.50
As the output shows sum of “Pressure” and “Temperature” column of dataframe.