Omit Missing Values in One Specific Data Frame Variable in R (Example Code)
On this page, I’ll show how to omit NA values in only one specific data frame variable in R programming.
Construction of Example Data
data(iris) # Preparing example data iris_NA <- head(iris) iris_NA$Sepal.Length[c(1, 3, 6)] <- NA iris_NA$Sepal.Width[c(2, 3, 4)] <- NA iris_NA # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 NA 3.5 1.4 0.2 setosa # 2 4.9 NA 1.4 0.2 setosa # 3 NA NA 1.3 0.2 setosa # 4 4.6 NA 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 NA 3.9 1.7 0.4 setosa |
data(iris) # Preparing example data iris_NA <- head(iris) iris_NA$Sepal.Length[c(1, 3, 6)] <- NA iris_NA$Sepal.Width[c(2, 3, 4)] <- NA iris_NA # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 NA 3.5 1.4 0.2 setosa # 2 4.9 NA 1.4 0.2 setosa # 3 NA NA 1.3 0.2 setosa # 4 4.6 NA 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 NA 3.9 1.7 0.4 setosa
Example: Applying is.na() Function to Remove NA Values in Particular Column of Data Frame
iris_NA_drop <- iris_NA[!is.na(iris_NA$Sepal.Length), ] # Omitting missing values in specific variable iris_NA_drop # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 2 4.9 NA 1.4 0.2 setosa # 4 4.6 NA 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa |
iris_NA_drop <- iris_NA[!is.na(iris_NA$Sepal.Length), ] # Omitting missing values in specific variable iris_NA_drop # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 2 4.9 NA 1.4 0.2 setosa # 4 4.6 NA 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa
Related Articles & Further Resources
Please have a look at the following R programming tutorials. They focus on similar topics as this article: