The rowSums() method calculates the sum of each row of a numeric array, matrix, or data frame. The colSums() method calculates the sum of each column of a numeric data frame, matrix, or array. The colMeans() method calculates the mean of each column of a numeric array, matrix, or data frame.
rowSums() Function in R
The rowSums() is an inbuilt R function used to calculate the sum of rows of a matrix or an array. To create a row sum and a row product column in an R data frame, use the rowSums() function and the star sign (*) for the product of column values inside the transform function.
Syntax
rowSums(x, na.rm=FALSE, dims=1L,...)
Parameters
x: Raster* object.
na.rm: It is a logical argument. If TRUE, NA values are ignored.
dims: this argument is ignored.
…: additional arguments (none implemented).
Example
To create a Matrix in R, use the matrix() function and pass the data and dimensions.
rv <- rep(1:4)
mtrx <- matrix(rv, 2, 2)
The rep() function replicates numeric values, or text, or the values of a vector for a specific number of times.
The matrix() function will create a 2 X 2 matrix.
To calculate the sum of row values, use the rowSums() function.
rv <- rep(1:4)
mtrx <- matrix(rv, 2, 2)
mtrx
cat("The sum of rows is: ", "\n")
rowSums(mtrx)
Output
[,1] [,2]
[1,] 1 3
[2,] 2 4
The sum of rows is:
[1] 4 6
You can see that the sum of row values 1 and 3 is 4, and 2 and 4 are 6. That is why we get the output of 4 and 6.
Calculate the sum of rows of the array in R
To create an array in R, use the array() function. Let’s create an array and use the rowSums() function to calculate the sum of rows of the array.
arr <- array(1:12, c(2, 3, 3))
arr
cat("The sum of rows is: ", "\n")
rowSums(arr)
Output
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
The sum of rows is:
[1] 45 54
You can pass the dim parameter to the rowSums() function.
arr <- array(1:12, c(2, 3, 3))
cat("The sum of rows is: ", "\n")
rowSums(arr, dim=2)
Output
The sum of rows is:
[,1] [,2] [,3]
[1,] 9 15 21
[2,] 12 18 24
Calculate the sum of rows of a data frame in R
To create a data frame in R, use the data.frame() function. To calculate the sum of rows of the data frame, use the rowSums() function.
x <- c(2:4)
y <- c(2:4 * 2)
z <- c(2:4 * 3)
w <- c(2:4 * 4)
df <- data.frame(x, y, z, w)
df
cat("The sum of rows of df is: ", "\n")
rowSums(df)
Output
x y z w
1 2 4 6 8
2 3 6 9 12
3 4 8 12 16
The sum of rows of df is:
[1] 20 30 40
Calculate the sum of rows of a data set in R
You can calculate the sum of rows of a dataset in R using the rowSums() function. We will use the USArrests dataset. Also, we will use the head() function to calculate the sum of the first 6 rows.
df <- head(USArrests)
rowSums(df)
Output
Alabama Alaska Arizona Arkansas California Colorado
328.4 365.5 413.1 268.3 416.6 328.6
How to Handle NA Values (na.rm) in rowSums() function
One of the most regular issues of the R rowSums() function is the existence of NAs (i.e., missing values) in the data. Let’s see what happens when we apply our functions to data with missing values.
x <- c(1, 2, NA, 3)
y <- c(NA, 4, 5, 6)
z <- c(7, NA, 8, 9)
w <- c(10, 11, NA, 13)
df <- data.frame(x, y, z, w)
df
cat("The sum of rows of df is: ", "\n")
rowSums(df)
Output
x y z w
1 1 NA 7 10
2 2 4 NA 11
3 NA 5 8 NA
4 3 6 9 13
The sum of rows of df is:
[1] NA NA NA 31
Most of our results contain NAs, and definitely, we do not want this.
But no worries, there is an easy solution. We have to add na.rm = TRUE within our functions.
x <- c(1, 2, NA, 3)
y <- c(NA, 4, 5, 6)
z <- c(7, NA, 8, 9)
w <- c(10, 11, NA, 13)
df <- data.frame(x, y, z, w)
cat("The sum of rows of df is: ", "\n")
rowSums(df, na.rm = TRUE)
Output
The sum of rows of df is:
[1] 18 17 13 31
As you can see that it ignored the NA values and calculate the sum of the remaining row values. Please note that the handling of missing values is a research topic by itself. Just ignoring NA values is usually not the best idea.
That is it for the rowSums() function in the R tutorial.