Sum of rows in R based on condition Last Updated : 21 Jul, 2025 Comments Improve Suggest changes Like Article Like Report To find the sum of rows in R based on a condition, we select only the rows that match the condition and then use the sum() function to add their values.Method 1: Sum of Rows Based on a Single ConditionWe calculate the sum of specific row values filtered by one logical condition.Example: 1team, captain, centuries: vectors used to define datadata.frame(): creates the data frame from vectorsdf: name of the data frameprint(): displays the data frame or resultwith(): applies expressions within a data frame contextsum(): calculates the total[captain == 'dhoni']: filters rows where captain equals "dhoni" R 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) print(df) print("Sum of rows based on the single condition") with(df, sum(centuries[captain == 'dhoni'])) Output:OutputExample: 2name, age, place: vectors used to define the datadf: name of the data frameplace == "guntur": filters rows where the place equals "guntur"sum(age[...]): sums the age values matching the condition R df = data.frame(name=c("rakesh", "ravi","mahesh","pavan","satish", "naresh"), age=c(12:17), place=c("nellore","palanadu","guntur","guntur","palanadu"," prakasam")) print(df) print("Sum of rows based on the single condition") with(df, sum(age[place == "guntur"])) Output:OutputMethod 2: Sum of Rows Based on Multiple ConditionsWe calculate the sum by applying multiple logical filters simultaneously.Example: 1employee, emp_id, salary: columns in the data frameemp_id > 7: filters rows with emp_id greater than 7employee == 'x': filters rows where employee is "x"&: logical AND operator to combine conditionssum(...): computes total for matching rows R 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)) print(df) print("Sum of rows based on the multiple conditions") with(df, sum(salary[emp_id > 7 & employee== 'x'])) Output:OutputExample: 2vec1, vec2, vec3: vectors used to create the data framevec2 == "a" & vec3 == "pk": compound condition for filtering rowssum(vec1[...]): computes the total of vec1 where both conditions are met R 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") df = data.frame(vec1, vec2, vec3) print(df) print("Sum of rows based on the multiple conditions") with(df, sum(vec1[vec2 == "a" & vec3== "pk"])) Output:OutputWe learned how to calculate the sum of rows based on conditions in R programming language. Comment More infoAdvertise with us Next Article Sum of column in R based on condition M maheshpe0b68 Follow Improve Article Tags : R Language R Functions R Data-science AI-ML-DS With R R Language +1 More Similar Reads Sum of column in R based on condition 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 sing 3 min read Sum of column in R based on condition 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 sing 3 min read Filter Rows Based on Conditions in a DataFrame in R To filter rows in a data frame using R, we can apply conditions directly to the columns. R offers several ways to perform this, depending on whether the condition is single or multiple.1. Filter Rows Based on a Single ConditionThis method filters rows where a specific condition is applied to a singl 2 min read How to Remove rows based on count of a specific value in R? Data cleaning is an essential step in data analysis, and removing rows based on specific criteria is a common task. One such criterion is the count of a specific value in a column. This article will guide you through the process of removing rows from a data frame in R based on the count of a specifi 5 min read Conditional Summation in PL/SQL Conditional Summation is a process of calculating a sum based on some specific criteria or condition. Calculating "conditional summation" can have lots of use cases. In financial sectors, it allows us to calculate the total or average of a specific category. Similarly in educational sectors, conditi 5 min read Take random sample based on groups in R R programming language provides us with many packages to take random samples from data objects, data frames, or data tables and aggregate them into groups. Method 1: Using plyr library The "plyr" library can be installed and loaded into the working space which is used to perform data manipulation an 4 min read Like