How to use sum() in R - Find the sum of elements in R | DigitalOcean (2024)

Let’s learn how to find the sum of the values with the help of the sum() in R. In this tutorial, we will try to find the sum of the elements of the vector.

The syntax of the sum() function is = sum(x,na.rm=FALSE/TRUE)

Vector is the easiest method to store multiple elements in R. Look at the below examples which show the various types of vectors.

Ex_vector: V<- c(2,4,6,8,10) #This is a numerical vector V<-c('red', 'blue', 'orange') #This is a character or string vector V<-c(TRUE, FALSE,TRUE) #This is a logical vector

Basic usage of sum() in R

In this section, we are finding the sum of the given values. Execute the below code to find the sum of the values.

#list of values or a vector having numerical valuesdf<- c(23,44,66,34,56,78,97,53,24,57,34,678,643,1344)#calculates the sum of the values sum(df)

Output —> 3231

Skip “NA” values when using the sum() function

Sometimes your dataset may contain 'NA" values i.e. ‘Not Available’. So if you add the values including NA, the sum() functions return the NA instead of numerical summation output.

Let’s learn how to deal with such datasets.

In this section, we are finding the sum of the vectors having the numeric values along with the value 'NA. The syntax of the sum() function shows that,

sum(x,na.rm=FALSE/TRUE)

  • x-> it is the vector having the numeric values.

  • na.rm-> This asks for remove or returns ‘NA’. If you made it TRUE, then it skips the NA in the vector, otherwise, NA will be calculated.

The below code will illustrate the action.

#creates a vector having numerical valuesx<-c(123,54,23,876,NA,134,2346,NA)#calculates the sum and removes the NA values from the summationsum(x,na.rm = TRUE)

Output —> 3556

#if you mention FALSE, the sum function returns the value NA sum(x,na.rm = FALSE)----> NA

Using sum() in R to add values of a specific column

Summing the values present in the particular column is very easy in R. The below code will illustrate the same.

This dataset contains the ‘NA’ value. So we are handling it by using na.rm=TRUE functon as shown in the code.

#read the data datasets::airquality#sample data, just a few samples Ozone Solar.R Wind Temp Month Day1 41 190 7.4 67 5 12 36 118 8.0 72 5 23 12 149 12.6 74 5 34 18 313 11.5 62 5 45 NA NA 14.3 56 5 56 28 NA 14.9 66 5 67 23 299 8.6 65 5 78 19 99 13.8 59 5 89 8 19 20.1 61 5 910 NA 194 8.6 69 5 10 continues.....#calculates the summation of the values in column 'Ozone'.sum(airquality$Ozone, na.rm = TRUE)

Output —> 4887

Sum all the data in all the rows independently in R

This section focuses on summing each row present in the dataset. Execute the below code to get the summed values of each row.

Here we are removing the NA values by na.rm=TRUE function.

datasets::airqualityrowSums(airquality, na.rm = TRUE)

Output: You can see the summation of all values present in each row.

[1] 311.4 241.0 255.6 413.5 80.3 119.9 407.6 203.8 122.1 286.6 103.9 367.7[13] 394.2 385.9 174.2 444.5 441.0 182.4 455.5 151.7 103.7 447.6 127.7 226.0[25] 169.6 369.9 97.0 148.0 426.9 457.7 435.4 379.6 378.7 334.1 289.2 324.6[37] 369.3 260.7 380.9 480.8 476.5 379.9 369.2 280.0 445.8 433.5 325.9 436.7[49] 155.2 241.5 262.3 260.3 164.7 200.6 362.3 249.0 245.0 163.3 223.5 157.9[61] 265.0 500.1 400.2 368.2 206.9 338.6 460.9 460.1 477.3 482.7 373.4 247.6[73] 380.3 317.9 417.9 171.3 418.9 425.3 461.3 384.1 406.5 131.9 377.7 418.5[85] 499.6 456.0 224.6 266.0 425.4 454.4 444.4 441.2 218.9 137.8 193.4 182.9[97] 140.4 171.6 485.0 434.3 432.0 340.6 253.5 353.5 415.5 333.7 177.5 204.3[109] 220.3 247.4 390.9 350.3 401.5 161.3 373.6 377.7 523.4 416.0 281.7 421.7[121] 476.3 461.3 412.3 370.9 383.1 363.8 390.6 250.4 238.5 378.9 348.3 354.9[133] 384.7 395.9 392.5 371.3 137.9 231.5 392.9 348.8 153.3 368.3 336.0 357.6[145] 148.2 298.3 168.3 147.6 334.9 271.2 331.3 271.0 361.5

Finding the sum of all the columns of the dataset

Let’s find the sum of each column present in the dataset. Execute the below code to find the sum of each column.

dataseta::airqualitycolSums(airquality, na.rm = TRUE)

Output:

Ozone Solar.R Wind Temp Month Day4887.0 27146.0 1523.5 11916.0 1070.0 2418.0

Wrapping up

The sum() function in R to find the sum of the values in the vector. This tutorial shows how to find the sum of the values, the sum of a particular row and column, and also how to get the summation value of each row and column in the dataset.

The important thing is to consider the NA values or not. If you want to eliminate it, mention TRUE otherwise it should be FALSE as shown above. That’s all for now, keep going!!!

How to use sum() in R - Find the sum of elements in R | DigitalOcean (2024)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5771

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.