Sum Across Multiple Rows and Columns Using dplyr Package in R
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 :
Calculating row sums
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.
Calculating column sums
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)