Sum Across Multiple Rows and Columns Using dplyr Package in R

14 Sep, 2021
In this article, we are going to see how to sum multiple Rows and columns using Dplyr Package in R Programming language.
The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : 
install.packages("dplyr")
The is.na() method in R is used to check if the variable value is equivalent to NA or not. This is important since the result of most of the arithmetic operations with NA value is NA. The replace() method in R can be used to replace the value of a variable in a data frame. This method is applied over the input data frame’s all cells and swapped with a 0 wherever found. 
Syntax: replace(data, replace-val)
The mutate() method is then applied over the output data frame, to modify the structure of the data frame by modifying the structure of the data frame. New columns or rows can be added or modified in the existing data frame. A new column name can be mentioned in the method argument and assigned to a pre-defined R function.
Syntax: mutate(new-col-name = rowSums(.))
The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame. 
Syntax: rowSums(.)
Code:
R
library("dplyr")  # creating a data framedata_frame <- data.frame(col1 = c(NA,2,3,4),                         col2 = c(1,2,NA,0),                         col3 = c(5,6,7,8)                         )print("Original DataFrame")print(data_frame)  # eliminating NA valuesdata_without_na <- data_frame %>%                        replace(is.na(.), 0) print("Row Wise Sum")  data_mod <- data_without_na%>%  mutate(sum_of_rows = rowSums(.))print(data_mod)
Output:
The NA values, if present, can be removed from the data frame using the replace() method in R. Successively, the data frame is then subjected to a method summarise_all() which is applied to every variable in the data frame. It takes as argument the function sum to calculate the sum over each column of the data frame. 
Syntax: summarise_all (sum) 
Code:
R
library("dplyr")  # creating a data framedata_frame <- data.frame(col1 = c(NA,2,3,4),                         col2 = c(1,2,NA,0),                         col3 = c(5,6,7,8)                         )print("Original DataFrame")print(data_frame)  # eliminating NA valuesdata_without_na <- data_frame %>%                        replace(is.na(.), 0) print("Column Wise Sum")  # computing column wise sumdata_mod <- data_without_na%>%  summarise_all(sum)  # printing final outputprint(data_mod)
Output:
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Printing Output of an R Program
How to Change Axis Scales in R Plots?
R - if statement