Substitute Missing Values by Row Means in R (Example Code)

In this R tutorial you’ll learn how to replace missing values by the corresponding row means.

Creation of Example Data

data(iris)                                                  # Loading iris data frame
iris_na <- iris[1:6, 1:4]
iris_na$Sepal.Length[c(1, 3, 5)] <- NA
iris_na
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1           NA         3.5          1.4         0.2
# 2          4.9         3.0          1.4         0.2
# 3           NA         3.2          1.3         0.2
# 4          4.6         3.1          1.5         0.2
# 5           NA         3.6          1.4         0.2
# 6          5.4         3.9          1.7         0.4

Example: Applying rowMeans() & is.na() Functions to Replace Missing Values by Row Means

iris_na <- iris_no_na                                       # Duplicate data frame
iris_no_na$Sepal.Length[is.na(iris_no_na$Sepal.Length)] <-  # Replacing missing data by row means
  rowMeans(iris_no_na, na.rm = TRUE)[is.na(iris_no_na$Sepal.Length)]
iris_no_na

Further Resources & Related Articles

Have a look at the following R programming tutorials. They illustrate topics such as descriptive statistics, variables, time objects, and missing data.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top