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
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 values
df<- 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
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 values
x<-c(123,54,23,876,NA,134,2346,NA)
#calculates the sum and removes the NA values from the summation
sum(x,na.rm = TRUE)
Output —> 3556
#if you mention FALSE, the sum function returns the value NA
sum(x,na.rm = FALSE)
----> NA
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 Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 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
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::airquality
rowSums(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
Let’s find the sum of each column present in the dataset. Execute the below code to find the sum of each column.
dataseta::airquality
colSums(airquality, na.rm = TRUE)
Output:
Ozone Solar.R Wind Temp Month Day
4887.0 27146.0 1523.5 11916.0 1070.0 2418.0
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!!!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.