Sum Across Multiple Rows & Columns Using dplyr Package in R (2 Examples)
In this R tutorial you’ll learn how to calculate the sums of multiple rows and columns of a data frame based on the dplyr package.
The article contains the following topics:
Let’s do this:
Example Data & Add-On Packages
First, we have to create some example data:
data <- data.frame(x1 = 1:5, # Example data x2 = c(NA, 5, 1, 1, NA), x3 = 9:5, x4 = c(4, 1, NA, 2, 8)) data # Print example data # x1 x2 x3 x4 # 1 1 NA 9 4 # 2 2 5 8 1 # 3 3 1 7 NA # 4 4 1 6 2 # 5 5 NA 5 8
Have a look at the previous output of the RStudio console. It shows that our exemplifying data contains five rows and four columns. Note that all of the variables are numeric and some of the variables contain NA values (i.e. missing values).
We also need to install and load the dplyr package, if we want to use the corresponding functions:
install.packages("dplyr") # Install & load dplyr library("dplyr")
Example 1: Sums of Columns Using dplyr Package
data %>% # Compute column sums replace(is.na(.), 0) %>% summarise_all(sum) # x1 x2 x3 x4 # 1 15 7 35 15
You can see the colSums in the previous output: The column sum of x1 is 15, the column sum of x2 is 7, the column sum of x3 is 35, and the column sum of x4 is 15.
Example 2: Sums of Rows Using dplyr Package
The following syntax illustrates how to compute the rowSums of each row of our data frame using the replace, is.na, mutate, and rowSums functions.
data %>% # Compute row sums replace(is.na(.), 0) %>% mutate(sum = rowSums(.)) # x1 x2 x3 x4 sum # 1 1 0 9 4 14 # 2 2 5 8 1 16 # 3 3 1 7 0 11 # 4 4 1 6 2 13 # 5 5 0 5 8 18
Have a look at the previous output: We have created a data frame with an additional column showing the sum of each row. Note that the NA values were replaced by 0 in this output.
Video & Further Resources
Do you need further explanations on the R programming codes of this tutorial? Then you may have a look at the following video of my YouTube channel. In the video, I show the R programming code of this tutorial in RStudio.
The YouTube video will be added soon.
In addition, you could read the related articles of my website. A selection of interesting articles is shown below.
- is.na Function in R
- sum Function in R
- Column & Row Sums with Base R
- Replace NA with 0
- Introduction to dplyr Package
- The R Programming Language
In this article, I showed how to use the dplyr package to compute row and column sums in the R programming language. In case you have any additional questions, don’t hesitate to let me know in the comments. In addition, please subscribe to my email newsletter in order to receive updates on the newest articles.