rowMeans() Function in R with Example

As a data scientist, you often work with a dataset with different categories, and sometimes you need to find the mean values of rows. The colMeans() method returns the mean for the specified columns for the data frame, matrix, or arrays.

The rowMeans() function is very useful when you want to find the mean values rows. But what is the rowMeans() function, and how to use it with numeric matrix, array, data frame, and dataset? Let’s find out in detail.

rowMeans() Function in R

The rowMeans() is an inbuilt R function that calculates the mean of each row of a matrix or array. The rowMeans() method 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

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, or text, or the values of a vector for a specific number of times.

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

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

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

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

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

How to Handle NA Values (na.rm) in rowMeans() function

One of the most regular issues of the R rowMeans() 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 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. 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 calculate the mean of the remaining column 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 colMeans() function in the R tutorial.

See also

rowSums() function in R

colSums() function in R

Median in R

Average in R

Calculate the square of Number in R

Leave a Comment