We will learn how to use the sum() function in the R programming language. This article will help you quickly find the sum of values in a dataframe or vector. Follow the syntax and code examples to get more helpful information.

**What is the sum() Function in R?**

If you want to calculate the sum of vector items, you can use the sum() function in R. The sum of a group can also be computed in R by putting it through the aggregate function.

The basic syntax for this function is as follows:

**sum() Function In R: The Syntax**

sum(data, na.rm=FALSE)

Parameter

**data:**a vector, names column of data frame, …**na.rm:**Whether or not to disregard NA values. The default value is FALSE.

**sum() Function In R: The Example**

**Using sum() Function with vector**

Here, we first need to create a vector to calculate sum() by using the sum() function. See the code below

# Create vectors x <- c(6, 5, 9, 12) y <- c(1.6, 9.5, -6.2, 5.5) z <- c(-3, 9, -1, 10) # Calculate the sum of vector x cat('The sum of xis:',sum(x)) # Calculate the sum of vector y cat('The sum of y is:',sum(y)) # Calculate the sum of vector z cat('The sum of z is:',sum(z)) # Calculate the sum of vector x, y, z cat('The sum is:',sum(x,y,z))

Output

```
The sum of x is: 32>
The sum of y is: 10.4>
The sum of z is: 15>
The sum is: 57.4
```

**Using sum() Function in a range**

This example, The range will calculate sum in a range

# Create in range x <- 5:10 y <- -10:0 # Calculate sum cat('The sum of x is:',sum(x)) cat('The sum of x is:',sum(y))

Output

```
The sum of x is: 45
The sum of x is: -55
```

**Using sum() Function with NA values**

Here, we can create a vector with NA values for this example, and then we will use ra.nm = TRUE to ignore the missing values.

# Create a vector x = c(5, 10, NA, 9, NA) # Calculate sum cat('The sum is:',sum(x,na.rm=TRUE))

Output

`The sum is: 24`

**Using sum() Function with a data frame**

First, we can create a data frame for this example as follows:

# Create a data frame df <- data.frame(A = c(9, 5, 3, 2, 8), B = c(7, 2, 1, 10, 8), C = c(5, 7, 4, 7, 8), D = c(2, 9, 9, 8, 8)) # View data frame df

Now, we will use the sum() function to calculate by using the names column of this data frame as follows

# Create a data frame df <- data.frame(A = c(9, 5, 3, 2, 8), B = c(7, 2, 1, 10, 8), C = c(5, 7, 4, 7, 8), D = c(2, 9, 9, 8, 8)) # Calculate sum A names column cat('The sum of the A names is:',sum(df$A)) # Calculate sum B names column cat('The sum of the B names is:',sum(df$B)) # Calculate sum C names column cat('The sum of the C names is:',sum(df$C)) # Calculate sum D names column cat('The sum of the D names is:',sum(df$D))

Output

```
The sum of the A names is: 27
The sum of the B names is: 28
The sum of the C names is: 31
The sum of the D names is: 36
```

As a similar vector, If we have NA values, we will use the sum() function with na.rm = TRUE.

# Create a data frame df <- data.frame(A = c(9, 5, 3, 2, 8), B = c(7, 2, 1, 10, 8), C = c(5, 7, 4, 7, 8), D = c(2, 9, 9, 8, 8), E = c(6, NA, 10, 9, NA)) # Calculate sum E names column cat('The sum of the E names is:',sum(df$A, na.rm=TRUE))

Output

`The sum of the E names is: 27`

**Summary**

The sum() function in R programming language to find the sum of the values in the vector and this article would share how to use the sum() function. So, if you have any questions, please comment below.

Have a great day!

## Leave a Reply