How to Substitute NA Values by Variable Mean in R (Example Code)
In this article you’ll learn how to exchange missing values by column means in one or multiple variables in the R programming language.
Example Data
data(iris) # Loading iris data set iris <- iris[ , 1:4] # Modifying iris data set iris$Sepal.Length[c(1, 3, 5)] <- NA iris$Sepal.Width[c(2, 3)] <- NA iris$Petal.Length[c(1, 4)] <- NA head(iris) # Printing head of iris data set # Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 NA 3.5 NA 0.2 # 2 4.9 NA 1.4 0.2 # 3 NA NA 1.3 0.2 # 4 4.6 3.1 NA 0.2 # 5 NA 3.6 1.4 0.2 # 6 5.4 3.9 1.7 0.4 |
data(iris) # Loading iris data set iris <- iris[ , 1:4] # Modifying iris data set iris$Sepal.Length[c(1, 3, 5)] <- NA iris$Sepal.Width[c(2, 3)] <- NA iris$Petal.Length[c(1, 4)] <- NA head(iris) # Printing head of iris data set # Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 NA 3.5 NA 0.2 # 2 4.9 NA 1.4 0.2 # 3 NA NA 1.3 0.2 # 4 4.6 3.1 NA 0.2 # 5 NA 3.6 1.4 0.2 # 6 5.4 3.9 1.7 0.4
Example: Using zoo Package to Exchange Missing Values in All Variables
install.packages("zoo") # Install zoo package library("zoo") # Load zoo |
install.packages("zoo") # Install zoo package library("zoo") # Load zoo
iris <- na.aggregate(iris) # Replacing missing data in all columns head(iris) # Printing head of updated iris data set # Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 5.861905 3.500000 3.789189 0.2 # 2 4.900000 3.056757 1.400000 0.2 # 3 5.861905 3.056757 1.300000 0.2 # 4 4.600000 3.100000 3.789189 0.2 # 5 5.861905 3.600000 1.400000 0.2 # 6 5.400000 3.900000 1.700000 0.4 |
iris <- na.aggregate(iris) # Replacing missing data in all columns head(iris) # Printing head of updated iris data set # Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 5.861905 3.500000 3.789189 0.2 # 2 4.900000 3.056757 1.400000 0.2 # 3 5.861905 3.056757 1.300000 0.2 # 4 4.600000 3.100000 3.789189 0.2 # 5 5.861905 3.600000 1.400000 0.2 # 6 5.400000 3.900000 1.700000 0.4
Further Resources
Have a look at the following R programming language tutorials. They explain topics such as data inspection and missing data: