Sum Function In R: How To Use sum() In R

Sum Function in R

What does sum () do in R?

The sum() function in R is used to calculate the sum of all elements which are numeric in a vector. It also can calculate the sum of elements in a row or a column in the data frame.

Syntax

sum(list, na.rm = FALSE)

Parameters:

• list: A vector that has numeric elements.
• na.rm: Optional. The default is FALSE. Whether the NA value should be removed if it exists. If not, the function will return NA.

How to use sum() in R

By the sum() function, you can calculate the sum of all elements in vectors, columns, or rows in the data frame. You will learn about that in the next title below.

Use the sum() function with the vector

We can calculate all elements which are numeric or NA in a vector by the sum() function.

age <- c(1, 2, 3, 4, 5, 6, NA)

# Default na.rm = FALSE
sum(age)
sum(age, na.rm = TRUE)

Output

[1] NA
[1] 21

Use the sum() function with the data frame

To learn how to use the sum() function with the data frame, create a data frame first. This is a data frame about the payroll of a company.

# Create the data frame
payroll = data.frame(
Level = c(
"Editor",
"Editor",
"SEO Editor",
"Editor",
"Author",
"Author",
"Author",
"Author",
"Author"
),
Name = c(
"Toi Pham",
"Hai Khanh",
"Hoe Doan",
"Linda",
"Peter",
"Ronaldo",
"Messi",
"Benzema",
"David"
),
Salary = c(1000, 2000, 5000, 800, 900, 800, 750, 700, 650),
Bonus = c(200, 300, 1000, 100, 100, 100, 100, 100, 100)
)

# Show the data frame
payroll

Output

      Level      Name   Salary Bonus
1     Editor  Toi Pham   1000   200
2     Editor Hai Khanh   2000   300
3 SEO Editor  Hoe Doan   5000  1000
4     Editor     Linda    800   100
5     Author     Peter    900   100
6     Author   Ronaldo    800   100
7     Author     Messi    750   100
8     Author   Benzema    700   100
9     Author     David    650   100

Use the sum() function with a column

The sum() function takes the column name as a parameter and calculates the sum of all elements in it.

Look at the example below.

# Calculate the sum of the bonus
sum(payroll\$Bonus)

Output

[1] 2100

Use the sum() function with multiple columns

You can use the sum() function to calculate the sum of all elements in each column by mapply() method.

Look at the example below.

# Calculate the sum of salary and the sum of bonus
mapply(sum, payroll[,c(-1,-2)])

Output

Salary  Bonus
12600   2100 

Use the sum() function with a row along with the ‘dplyr’ package

We can calculate the sum of all elements in a row with the specified columns by the sum() function with the ‘dplyr’ package.

Look at the example below.

# Calculate the Total Wage
library(dplyr)

payroll %>%
rowwise() %>%
mutate(
Total = sum(c(Salary, Bonus))
)

Output

  Level      Name      Salary Bonus Total
<chr>      <chr>      <dbl> <dbl> <dbl>
1 Editor     Toi Pham    1000   200  1200
2 Editor     Hai Khanh   2000   300  2300
3 SEO Editor Hoe Doan    5000  1000  6000
4 Editor     Linda        800   100   900
5 Author     Peter        900   100  1000
6 Author     Ronaldo      800   100   900
7 Author     Messi        750   100   850
8 Author     Benzema      700   100   800
9 Author     David        650   100   750

Summary

You have learned how to use sum() in R. By this function, you can calculate the sum of all elements in vectors, columns, or rows in the data frame. If you have any questions about this function, feel free to leave your comments below, and we will answer your questions. Thanks!

Maybe you are interested:

Posted in R