Open In App

Sum of column in R based on condition

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to calculate the sum of columns based on conditions by using R Programming Language.

How to calculate the sum of columns?

R language offers various methods or functions to calculate the sum of columns based on condition. By using these methods, can work more efficiently. Some of the methods are:

You can use the following basic syntax to sum column based on condition in R:

#sum values in column 3 where col1 is equal to 'U'

sum(df[which(df$col1=='U'), 3])

A sum of columns based on a single condition

In the below example, we created a dataframe and calculated the sum of column based on single condition.

R
Name=c("U","U","U","V","V","W")
Score=c(10,20,30,40,50,60)
Wickets=c(2,3,4,5,6,7)

df = data.frame(Name, Score, Wickets)
print(df)

#sum of values in column 2 where name is equal to U
print("sum of required column is")
sum(df[which(df$Name=='U'),2])

Output:

  Name Score Wickets
1 U 10 2
2 U 20 3
3 U 30 4
4 V 40 5
5 V 50 6
6 W 60 7

[1] "sum of required column is"
[1] 60

In the below example, we created a dataframe and calculated the sum of column based on single condition.

R
Name=c("U","U","U","V","V","W")
Score=c(10,20,30,40,50,60)
Wickets=c(2,3,4,5,6,7)

df = data.frame(Name, Score, Wickets)
print(df)

#sum of values in column 3 where name is equal to U
print("sum of required column is")
sum(df[which(df$Name=='V'),3])

Output:

  Name Score Wickets
1 U 10 2
2 U 20 3
3 U 30 4
4 V 40 5
5 V 50 6
6 W 60 7

[1] "sum of required column is"
[1] 11

Sum of column based on multiple conditions

In the below example, we created a dataframe and calculated the sum of column based on multiple conditions.

R
name=c("U","U","W","V","V","U")
place=c("uk","uk","rk","rk","rk","nk")
score=c(10,20,30,40,50,60)
wickets=c(2,3,4,5,6,7)

#creating data frame
df <- data.frame(name, place, score, wickets)

print(df)

print(" sum of column based on multiple coditions is")
sum(df[which(df$name=='U' & df$place=='uk'), 3])

Output:

  name  place  score  wickets
1 U uk 10 2
2 U uk 20 3
3 W rk 30 4
4 V rk 40 5
5 V rk 50 6
6 U nk 60 7

[1] " sum of column based on multiple conditions is"
[1] 30

In the below example, we created a dataframe and calculated the sum of column based on a multiple conditions.

R
name=c("U","U","W","V","V","U")
place=c("uk","uk","rk","rk","rk","nk")
score=c(10,20,30,40,50,60)
wickets=c(2,3,4,5,6,7)

#creating data frame
df <- data.frame(name, place, score, wickets)

print(df)

print(" sum of column based on multiple conditions is")
sum(df[which(df$name=='V' & df$place=='rk'), 3])

Output:

    name   place   score  wickets
1 U uk 10 2
2 U uk 20 3
3 W rk 30 4
4 V rk 40 5
5 V rk 50 6
6 U nk 60 7

[1] " sum of column based on multiple conditions is"
[1] 90

Conclusion

In conclusion, we learned about how to calculate the sum of a column based on different conditions. R language offers versatile tools while calculating sum of column based on various conditions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads