How to Use sum Function in R?
Last Updated :
20 Dec, 2021
In this article, we will discuss how to use the sum() function in the R Programming Language.
sum() function: This is used to return the total/sum of the given data
Syntax:
sum(data)
Arguments:
- data can be a vector or a dataframe
Example 1: Using sum() function to calculate the sum of vector elements
In this method, the user has to simply call the sum() function passed with the parameter as the numeric vector, which will be returning the sum of all the integers present inside the function in the R language.
Syntax:
sum(vector_name)
where, vector_name: name of the numeric vector to calculate sum.
Example 1:
In this example, we will be finding the sum of given Values in Vector with 20 elements from 1 to 20 in R language.
Output:
[1] 210
Example 2: Using the sum() function to calculate the sum of the dataframe columns
In this method to calculate the sum of the data frame columns, the user has to call the sum function with the name of the dataframe column passed as the parameter with it and further this will be returning in the sum of the all the integer elements mentioned in the given column.
Syntax:
sum(dataframe$column)
where,
- dataframe: name of the data frame
- column: name of the column of the data frame where sum is to be calculated
Example 2:
In this example, we will be computing the sum of given values in dataframe column with 3 columns and we are going to find the sum in first three columns separately in R language.
R
data= data.frame (col1= c (1:20),col2= c (21:40),
col3= c (41:60))
sum (data$col1)
sum (data$col2)
sum (data$col3)
|
Output:
[1] 210
[1] 610
[1] 1010
Example 3: Using the sum() function to compute the sum of multiple columns
In this approach to calculate the sum of multiple columns, the user has to call the sapply() function with the sum argument into in and the name of the required columns mentioned within a vector as the arguments to this function, further this will be resulting to the sum of the all the multiples columns passed to the user in the R programming language.
Syntax:
sapply(dataframe[ , c('column1', 'column2',.,'column n)], sum)
Example:
Here, in this example, we are going to create a dataframe with 3 columns ad find the sum of three columns at a time using sapply() function.
R
data= data.frame (col1= c (1:20),col2= c (21:40),col3= c (41:60))
sapply (data[ , c ( 'col1' , 'col2' , 'col3' )], sum)
|
Output:
col1 col2 col3
210 610 1010
Example 4: Using sum() function to compute sum of vector elements with NA values
In this method, we will be using the sum() function with the NA (not a number) values present within the given vector. Here the user has to pass the na.rm argument within the function to remove all the NA values present and to just calculate the sum of the integers present in the given vector, further this will be returning the sum of all the integers only present in the given vector.
Syntax:
sum(vector,na.rm=TRUE)
Example:
In this example, we will be firstly creating a vector with 10 elements that include 4 NA values and further with the help of the sum() function and the na.rm arguments we will be computing the sum of the elements present in the vector.
R
data= c ( NA , NA ,1,2,3,4,5,6, NA , NA )
sum (data,na.rm= TRUE )
|
Output:
[1] 21
Example 5: Using sum() function to compute row-wise sum of the dataframe
In this method, the user needs to install and import the dplyr package, here this package is responsible to give the access of its functionalities to the user, then the user needs to foolw the below given suntax with the sum function accordingly to get the row-wise sum and store the result in another new column using the mutate() function in the R programming language.
Syntax:
data%>%rowwise() %>%mutate(new_column = sum(c(columns)))
where,
- data is the input dataframe
- columns are the columns to perform sum operation
Syntax to install and import the dplyr package:
install.package("dplyr")
library("dplyr")
Example:
In this example, we will be creating a dataframe with 3 columns and add a first and third column using the sum and the mutate() function in the R language.
R
library (dplyr)
data= data.frame (col1= c (1:5),col2= c (21:25),col3= c (41:45))
data%>% rowwise () %>% mutate (Total_Sum = sum ( c (col1,col3)))
|
Output:

Example 6: Using sum() function to compute the sum of the column by group
In this method to compute the sum of the columns by the group of the data frames, the user needs to install and import the dplyr package and then call the aggregate function from the dplyr package with the required parameters passed within in it, here by aggregate function the grouping data in one column will take place and then with the sum() function the sum will be calculate of the grouped columns in the R language.
Syntax to install and import the dplyr package:
install.package("dplyr")
library("dplyr")
Syntax:
aggregate(dataframe$column_name, by= list(dataframe$group_column), FUN=sum)
where
- dataframe is the input dataframe
- column_name is the column to get sum
- group_column is the column to be grouped
- FUN specifies sum parameter to get sum operation
Example:
In this example, we will be looking at R program to create three columns and perform sum operation to compute the sum of the columns by groups using aggregate() function in the R language.
R
library (dplyr)
data= data.frame (col1= c ( "java" , "java" , "php" , "python" , "python" ),
col2= c (21:25),col3= c (41:45))
aggregate (data$col3, by= list (data$col1), FUN=sum)
|
Output:
Similar Reads
How to use Summary Function in R?
The summary() function provides a quick statistical overview of a given dataset or vector. When applied to numeric data, it returns the following key summary statistics: Min: The minimum value in the data1st Qu: The first quartile (25th percentile)Median: The middle value (50th percentile)3rd Qu: Th
2 min read
How to Use Nrow Function in R?
In this article, we will discuss how to use Nrow function in R Programming Language. This function is used in the dataframe or the matrix to get the number of rows. Syntax: nrow(data) where, data can be a dataframe or a matrix. Example 1: Count Rows in Data Frame In this example, we are going to cou
2 min read
How to use the source Function in R
In this article, we will be looking at the practical implementation of the source function in the R programming language. Source Function: Source function in R is used to use functions that are created in another R script. The syntax of this function is given below: source("Users/harsh/Desktop/Geeks
2 min read
How to use the NumPy sum function?
NumPy's sum() function is extremely useful for summing all elements of a given array in Python. In this article, we'll be going over how to utilize this function and how to quickly use this to advance your code's functionality. Let's go over how to use these functions and the benefits of using this
4 min read
How to Use Gather Function in R
In data analysis and manipulation, it's often necessary to reshape datasets for better comprehension or analysis. The gather() function in the R Programming Language part of the tidyr package, is a powerful tool for reshaping data from wide to long format. This article will explore the gather() func
4 min read
How to Use aggregate Function in R
In this article, we will discuss how to use aggregate function in R Programming Language. aggregate() function is used to get the summary statistics of the data by group. The statistics include mean, min, sum. max etc. Syntax: aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)
2 min read
How to Use file.path() Function in R
R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr
3 min read
sum() function in R
sum() function in R Programming Language returns the addition of the values passed as arguments to the function. Syntax: sum(...) Parameters: ...: numeric or complex or logical vectorssum() Function in R ExampleR program to add two numbersHere we will use sum() functions to add two numbers. [GFGTABS
2 min read
How to Perform a SUMIF Function in R?
In this article, we will discuss the sumif function in R Programming Language. This function is used to group the data and get the sum of the values by a group of values in the dataframe, so we are going to perform this operation on the dataframe. Method 1: Perform a SUMIF Function On One Column: In
3 min read
ts Function In R
The ts function in R Programming Language is used to create time series objects, which are data structures designed for time-related data. Time series data consists of observations over a period, often at regular intervals, such as daily, weekly, monthly, or yearly data. Time series analysis is cruc
4 min read