![]() |
VOOZH | about |
Sum of Column in R based on condition refers to calculating the total of values in a column after applying specific conditions. This helps in filtering the data before performing the summation. Logical conditions are used with indexing or the subset() function along with sum(). It supports both single and multiple conditions.
Syntax:
sum(dataframe[which(condition), column_number])
Parameters:
We can calculate the sum of specific columns by applying a condition to filter the data first.
Example 1: We create a dataframe and calculate the sum of the Score column where the Name is equal to 'U'.
Output:
[1] 60
Example 2: We calculate the sum of the Wickets column where the Name is equal to 'V'.
Output:
[1] 11
We can calculate the sum of a column by applying multiple conditions to filter the data before performing the calculation.
Example 1: We calculate the sum of the score column based on multiple conditions: name is 'U' and place is 'uk'.
Output:
[1] 30
Example 2: We now sum the score values where name is 'V' and place is 'rk'.
Output:
[1] 90
In this article, we calculated the sum of a column in R using both single and multiple conditions.