![]() |
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 conditionThis 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.
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.
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 conditionsThis 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.
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.
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 ConclusionIn 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. |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |