Sum function in R – sum(), is used to calculate the sum of vector elements. sum of a particular column of a dataframe. sum of a group can also calculated using sum() function in R by providing it inside the aggregate function. with sum() function we can also perform row wise sum using dplyr package and also column wise sum lets see an example of each.

- sum of the list of vector elements with NA values
- Sum of a particular column of the dataframe in R
- column wise sum of the dataframe using sum() function
- Sum of the group in R dataframe using aggregate() and dplyr package
- Row wise sum of the dataframe in R using sum() function

**Syntax for sum function :**

**x –**numeric vector**rm-**whether NA should be removed, if not, NA will be returned

** **

**Example of sum function in R**

sum of vectors is depicted below.

# R sum function sum(1:10) sum(c(2,5,6,7,1,2))

**output:**

**[1] 55**

**[1] 23**

** **

** **

**Example of sum function with NA:**

sum() function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in sum() function

# sum() function in R for input vector which has NA. x = c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA) sum(x,na.rm=TRUE)

**output:**

**[1] 2.463**

** **

**Example of sum() function in R dataframe: **

Lets create the data frame to demonstrate sum function – sum() in r

### create the dataframe my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"), Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120), Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3)) my_basket

so the resultant dataframe will be

** sum of a column in R data frame using sum() function :**

sum() function takes the column name as argument and calculates the sum of that particular column

# sum() function in R : sum of a column in data frame sum(my_basket$Price)

so the resultant sum of “Price” column will be

**output:**

**[1] 945**

**column wise sum using sum() function:**

sum() function is applied to the required column through mapply() function, so that it calculates the sum of required column as shown below.

# sum() function in R : sum of multiple column in data frame mapply(sum,my_basket[,c(-1,-2)])

so the resultant sum of “Price” and “Tax” columns will be

#### Sum of the column by group using sum() function

aggregate() function along with the sum() function calculates the sum of a group. here sum of “Price” column, for “Item_Group” is calculated.

##### Sum of the column by group aggregate(x= my_basket$Price, by= list(my_basket$ITEM_GROUP), FUN=sum)

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. sum of price for each group is calculated as shown below

#### Sum of the column by group and populate it by using sum() function:

group_by() function along with the sum() function calculates the sum of a group. here sum of “Price” column, for “Item_Group” is calculated and populated across as shown below

#### sum of the column by group and populate it using dplyr library(dplyr) my_basket %>% group_by(ITEM_GROUP) %>% mutate(sum_by_group = sum(Price))

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. sum of price for each group is calculated and populated as shown below

#### Row wise sum using sum() function along with dplyr

Row wise sum is calculated with the help rowwise() function of dplyr package and sum() function as shown below

## row wise sum using dplyr library(dplyr) my_basket %>% rowwise() %>% mutate( Total_price = sum(c(Price,Tax)) )

row wise sum of “Price” and “Tax” is calculated and populated for each row as shown below

For further understanding of sum() function in R using dplyr one can refer the dplyr documentation

**Related Topics:**