Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created June 19, 2025 07:05
Show Gist options
  • Select an option

  • Save graebnerc/66573b15384b77c7e68b12358b59fb3b to your computer and use it in GitHub Desktop.

Select an option

Save graebnerc/66573b15384b77c7e68b12358b59fb3b to your computer and use it in GitHub Desktop.
Filter Missing Values Example
# dyplyr is used for such cases:
library(dplyr)
# Create a small dataset with missing values:
students <- data.frame(
name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
age = c(20, NA, 22, 19, NA),
grade = c(85, 92, NA, 78, 88),
city = c("Boston", "Chicago", NA, "Denver", "Austin")
)
# Display original dataset
print(students)
# Filter rows where specific column is NOT missing
students_with_age <- students %>%
filter(!is.na(age))
print("\nStudents with age data:")
print(students_with_age)
# Filter rows where multiple columns are NOT missing
students_age_and_grade <- students %>%
filter(!is.na(age) & !is.na(grade))
print("\nStudents with both age and grade:")
print(students_age_and_grade)
# Digression: Filter rows with NO missing values (complete cases)
complete_data <- students %>%
filter(complete.cases(.))
print("\nRows with no missing values:")
print(complete_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment