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 sum of the rows easily. Some of the methods to perform the sum of rows based on the condition are:
Method 1: Sum of rows based on the single condition
This method calculates the sum of rows based on a single condition. In the below example, we created a data frame and performed the sum of rows based on a single condition.
team=c("csk", "rcb","kxip","csk","gt","rcb")
captain=c("dhoni","kohli","rahul","dhoni","hardik","kohli")
centuries=c(70,64,50,70,35,43)
df = data.frame(team,captain,centuries)
#printing data frame
print(df)
print("Sum of rows based on the single condition")
#sum of scores
with(df, sum(centuries[captain == 'dhoni']))
Output:
team captain centuries
1 csk dhoni 70
2 rcb kohli 64
3 kxip rahul 50
4 csk dhoni 70
5 gt hardik 35
6 rcb kohli 43
[1] "Sum of rows based on the single condition"
[1] 140
In the below example, we created a data frame and performed sum of rows based on a single condition.
df = data.frame(name=c("rakesh", "ravi","mahesh","pavan","satish", "naresh"),
age=c(12:17),
place=c("nellore","palanadu","guntur","guntur","palanadu"," prakasam"))
#view data frame
print(df)
print("Sum of rows based on the single condition")
with(df, sum(age[place == "guntur"]))
Output:
name age place
1 rakesh 12 nellore
2 ravi 13 palanadu
3 mahesh 14 guntur
4 pavan 15 guntur
5 satish 16 palanadu
6 naresh 17 prakasam
[1] "Sum of rows based on the single condition"
[1] 29
Method 2: Sum of rows based on the multiple conditions
This method calculates the sum of rows based on a multiple conditions. In the below example, we created a data frame and performed sum of rows based on the multiple conditions.
#creating data frame
df = data.frame(employee = c("x", "y","y","w", "x","z","z"),
emp_id = c(4, 7, 7, 4,15, 12, 12),
salary = c(30000, 45000, 25000, 40000, 60000, 70000, 55000))
#printing data frame
print(df)
print("Sum of rows based on the multiple conditions")
with(df, sum(salary[emp_id > 7 & employee== 'x']))
Output:
employee emp_id salary
1 x 4 30000
2 y 7 45000
3 y 7 25000
4 w 4 40000
5 x 15 60000
6 z 12 70000
7 z 12 55000
[1] "Sum of rows based on the multiple conditions"
[1] 60000
In the below example, we created a data frame and performed sum of rows based on the multiple conditions.
vec1=c(4,4,6,7,8,8,9)
vec2=c("a","a","c","e","e","f","g")
vec3=c("pk","pk","sk","pk","mk","ak","kk")
#creating data frame
df = data.frame(vec1, vec2, vec3)
#view data frame
print(df)
print("Sum of rows based on the multiple conditions")
with(df, sum(vec1[vec2 == "a" & vec3== "pk"]))
Output:
vec1 vec2 vec3
1 4 a pk
2 4 a pk
3 6 c sk
4 7 e pk
5 8 e mk
6 8 f ak
7 9 g kk
[1] "Sum of rows based on the multiple conditions"
[1] 8
Conclusion
In Conclusion, we learned about how to calculate sum of rows based on condition in R. R language provides versatile tools while calculating the sum of rows based on condition.