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.



Similar Reads

Sum of rows in R based on condition
In this article, we will explore various methods to perform the sum of rows in R based on conditions using the R Programming Language. How to calculate the sum of rows based on the condition?R language offers various methods to calculate the sum of rows based on the condition. By using these methods provided by R, it is possible to calculate the su
3 min read
Replace Values Based on Condition in R
In this article, we will examine various methods to replace values based on conditions in the R Programming Language. How to replace values based on conditionR language offers a method to replace values based on conditions efficiently. By using these methods provided by R, it is possible to replace values based on condition easily. Some of the meth
3 min read
Sum of rows based on column value in R dataframe
In this article, we will be discussing how we can sum up row values based on column value in a data frame in R Programming Language. Suppose you have a data frame like this: fruits shop_1 shop_2 1. Apple 1 13 2. Mango 9 5 3. Strawberry 2 14 4. Apple 10 6 5. Apple 3 15 6. Strawberry 11 7 7. Mango 4 16 8. Strawberry 12 8 This dataset consists of frui
2 min read
Extract specific column from a DataFrame using column name in R
In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language. In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is, after extracting the data from column we have to s
5 min read
How to change row values based on a column value in R dataframe ?
In this article, we will see how to change the values in rows based on the column values in Dataframe in R Programming Language. Syntax: df[expression ,] &lt;- newrowvalue Arguments : df - Data frame to simulate the modification uponexpression - Expression to evaluate the cell data based on a column valuenewrowvalue - The modified value to replace
4 min read
How to add a column based on other columns in R DataFrame ?
A data frame can be accessed and modified to store new insertions and deletions. The data frame can undergo mutations to increase its dimensions and store more data, as well as the rows and columns values, which can be modified based on other cell values. In this article, we will see how to add columns based on other columns in DataFrame in R Progr
5 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all components. Every element is the same as each of the value
3 min read
Condition Handling in R Programming
Decision handling or Condition handling is an important point in any programming language. Most of the use cases result in either positive or negative results. Sometimes there is the possibility of condition checking of more than one possibility and it lies with n number of possibilities. In this article let's discuss how condition handling works i
5 min read
Select rows of a matrix in R that meet a condition
A large dataset is often required to be filtered according to our requirements. In this article, we will be discussing how we can select a row from a matrix in R that meets the condition. For better understanding let's understand the problem statement with the help of an example. Example: Data in use: car_models car_type car_color year 1 Maruti Die
2 min read
Filter DataFrame columns in R by given condition
In this article, we are going to see how to select DataFrame columns in R Programming Language by given condition. R data frame columns can be subjected to constraints, and produce smaller subsets. However, while the conditions are applied, the following properties are maintained : Rows of the data frame remain unmodified.Data frame attributes are
5 min read
Article Tags :