What is rowMeans() Function in R (5 Examples)

The rowMeans() is a built-in R function that calculates the mean of each row of a matrix or array. The syntax of the rowMeans function is rowMeans(x, na.rm=FALSE), where x is the name of the matrix or data frame, and na.rm is whether to ignore NA values and returns the mean for the specified rows for the data frame, matrix, or arrays.

Syntax

rowMeans(x, na.rm = FALSE, dims = 1)

Parameters

x: It is an array of two or more dimensions containing numeric, complex, integer, or logical values or a numeric data frame.

dims: It is an integer: Which dimensions are regarded as ‘columns’ to sum over. It is over dimensions 1:dims.

na.rm: It is a logical argument. If TRUE, NA values are ignored.

Example 1: Finding rowMeans 

Let’s create a Matrix using the matrix() function and calculate the mean of rows of the matrix.

rv <- rep(1:4)

mtrx <- matrix(rv, 2, 2)
mtrx
cat("The mean of rows is: ", "\n")
rowMeans(mtrx)

Output

     [,1] [,2]
[1,]   1    3
[2,]   2    4

The mean of rows is:

[1] 2  3

The rep() function replicates numeric values, text, or the values of a vector a specific number of times.

The matrix() function will create a 2 X 2 matrix.

The mean of the first row values is 1, 3 cause 1 + 3 = 4 and 4 / 2 = 2, and the same for the second column whose output is 3.

Example 2: Calculate the mean 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 rowMeans() function to calculate the mean of rows of the array.

arr <- array(1:4, c(2, 2, 2))
arr
cat("The mean of rows is: ", "\n")
rowMeans(arr)

Output

, , 1

     [,1] [,2]
[1,]   1    3
[2,]   2    4

, , 2

     [,1] [,2]
[1,]   1    3
[2,]   2    4

The mean of rows is:
[1] 2   3

Example 3: Calculate the mean of rows of a data frame in R

To create a data frame in R, use the data.frame() function. To calculate the mean of rows of the data frame, use the rowMeans() 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)
cat("The mean of rows of df is: ", "\n")
rowMeans(df)

Output

   x  y  z  w
1  2  4  6  8
2  3  6  9 12
3  4  8 12 16

The mean of rows of df is:

[1] 5.0  7.5  10.0

Example 4: Calculate the mean of rows of a data set in R

You can calculate the mean of rows of the dataset in R using the rowMeans() function. We will use the USArrests dataset.

rowMeans(USArrests)

Output

Calculate the mean of rows of a data set in R

Example 5: Handling NA Values (na.rm) in rowMeans() function

One of the most regular issues of the rowMeans() function is 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 mean of rows of df is: ", "\n")
rowMeans(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 mean of rows of df is:

[1] NA NA NA 7.75

You can see that we got all the NAs in the output because every column contains one NA. So, it will return NA in the output.

But no worries, there is an easy solution. First, 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 mean of rows of df is: ", "\n")
rowMeans(df, na.rm = TRUE)

Output

The mean of rows of df is:
[1] 6.000000  5.666667  6.500000  7.750000

As you can see that it ignored the NA values and calculated the mean of the remaining column values. Please note that handling missing values is a research topic in itself. Just ignoring NA values is usually not the best idea.

Conclusion

The rowMeans() function in R calculates the arithmetic mean of values across rows of a matrix or data frame. It takes as input a matrix or data frame and returns a numeric vector with the mean of values for each row.

See also

colMeans() function in R

rowSums() function in R

colSums() function in R

Leave a Comment