Open In App

How to Use sum Function in R?

Last Updated : 20 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

R




# create  vector
data=c(1:20)
 
# get the sum
sum(data)


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




# create dataframe with 3 columns
data=data.frame(col1=c(1:20),col2=c(21:40),
                col3=c(41:60))
 
# get the sum of column1
sum(data$col1)
 
# get the sum of column2
sum(data$col2)
 
# get the sum of column3
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




# create dataframe with 3 columns
data=data.frame(col1=c(1:20),col2=c(21:40),col3=c(41:60))
 
# get the sum of three columns
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




# create a vector
data=c(NA,NA,1,2,3,4,5,6,NA,NA)
 
# get the sum
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




# load the package
library(dplyr)
 
# create a dataframe with 3 columns
data=data.frame(col1=c(1:5),col2=c(21:25),col3=c(41:45))
 
# get sum of first and third column
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




# load the package
library(dplyr)
 
# create a dataframe with 3 columns
data=data.frame(col1=c("java","java","php","python","python"),
                col2=c(21:25),col3=c(41:45))
 
# get sum of third column by group with first column
aggregate(data$col3, by= list(data$col1), FUN=sum)


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads