Sum of rows based on column value in R dataframe Last Updated : 01 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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 fruits name and shop_1, shop_2 as a column name. Here shop_1 and shop_2 show the number of fruits available in shops. Now you want to find the aggregate sum of all the rows in shope_1 that have the same fruit value. So our dataset looks like this : fruits shop_1 shop_2 1. Apple 14 34 2. Mango 13 21 3. Strawberry 25 29 Method 1: Using aggregate function The aggregate function creates a subset of the original data and computes the statistical function for each subset and returns the result. Syntax: aggregate(.~fruit,data=df,FUN=sum) Example: R # Sum of rows based on column values # Creating dataset # creating fuits column x <- c("Apple","Mango","Strawberry", "Apple","Apple","Strawberry", "Mango","Strawberry") # creating shop_1 column y <- c(1,9,2,10,3,11,4,12) # creating shop_2 column z <- c(13,5,14,6,15,7,16,8) # creating dataframe df <- data.frame(fruits=x,shop_1=y,shop_2=z) # applying aggregate function aggregate(.~fruits,data=df,FUN=sum) Output: Using aggregate functionMethod 2: Using ddply and numcolwise function ddply simply split the given data frame and perform any operation on it (probably apply a function) and return the data frame. colwise is a function from the famous plyr package. colwise function is used to compute a function on each column in data frame, it computes column wise. Example: R # Sum of rows based on column values # loading library library(plyr) # Creating dataset # creating fuits column x <- c("toy1","toy2","toy3", "toy1","toy1","toy3", "toy2","toy3") # creating stock_1 column y <- c(1,2,3,4,5,6,4,8) # creating stock_2 column z <- c(9,1,10,5,2,6,4,8) # creating dataframe df <- data.frame(toys=x,stock_1=y,stock_2=z) # using sum function colwise ddply(df,"toys",numcolwise(sum)) Output: Using ddply Comment More infoAdvertise with us Next Article Select DataFrame Rows where Column Values are in Range in R A arpit122000 Follow Improve Article Tags : AI-ML-DS Experiences Internship Machine Learning Programming Language R Language R Programs R DataFrame-Programs R-DataFrame +5 More Practice Tags : Machine Learning Similar Reads How to find the sum of column values of an R dataframe? In this article, we are going to find the sum of the column values of a dataframe in R with the use of sum() function. Syntax: sum(dataframe$column_name) Creating a Dataframe A dataframe can be created with the use of data.frame() function that is pre-defined in the R library. This function accepts 2 min read Select DataFrame Rows where Column Values are in Range in R In this article, we will discuss how to select dataframe rows where column values are in a range in R programming language. Data frame indexing can be used to extract rows or columns from the dataframe. The condition can be applied to the specific columns of the dataframe and combined using the logi 2 min read How to filter R DataFrame by values in a column? In R Programming Language, dataframe columns can be subjected to constraints, and produce smaller subsets. However, while the conditions are applied, the following properties are maintained : Rows are considered to be a subset of the input.Rows in the subset appear in the same order as the original 5 min read Sum of Two or Multiple DataFrame Columns in R In this article, we will discuss how to perform some of two and multiple dataframes columns in R programming language. Database in use: Sum of two columns The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using 2 min read Select Odd and Even Rows and Columns from DataFrame in R In this article, we will discuss how to select odd and even rows from a dataframe in R programming language. Getting Odd Rows from the Data Frame The number of rows in a data frame in R can be fetched by the nrow() method. It returns the number of rows in the data frame. The seq_len() method is then 6 min read Select rows from a DataFrame based on values in a vector in R In this article, we will discuss how to select rows from a DataFrame based on values in a vector in R Programming Language. Method 1: Using %in% operator %in% operator in R, is used to identify if an element belongs to a vector or Dataframe. It is used to perform a selection of the elements satisfyi 5 min read Convert dataframe rows and columns to vector in R In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met 2 min read Select Rows if Value in One Column is Smaller Than in Another in R Dataframe In this article, we will discuss how to select rows if the value in one column is smaller than another in dataframe in R programming language. Data frame in use: Method 1: Using Square Brackets By using < operator inside the square bracket we can return the required rows. Syntax: dataframe[datafr 2 min read How to add column to dataframe in R ? In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec 2 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 componen 3 min read Like