Great! You want to check for missing values and confirm data types, and you'd like to use summary() and count() (from the tidyverse) to review your data.
Here's how to do that step-by-step:
✅ 1. Use summary() to get an overview
The summary() function gives a quick statistical summary of each column, including:
- For numeric columns: min, 1st quartile, median, mean, etc.
- For character/factor columns: frequency of top levels
- For dates: min, max, NAs
- Also shows number of
NAs if present
summary(lab_record_clean)
This will help you spot:
- Missing values (
NAs)
- Unexpected ranges or outliers
- Character variables that should be numeric or factor
✅ 2. Count missing values per column
To specifically count missing values:
map_dfr(lab_record_clean, ~ sum(is.na(.x))) %>%
t() %>%
as.data.frame()
Or using colSums:
data.frame(
Missing = colSums(is.na(lab_record_clean)),
row.names = "Variables"
)
This gives a table showing how many NAs are in each column.
✅ 3. Check data types with str() or glimpse()
str(lab_record_clean)
or
library(dplyr)
glimpse(lab_record_clean)
This will show:
- Each variable’s name
- Its type:
chr, num, int, Date, etc.
✅ 4. Use count() to check categorical variables
If you have categorical variables (e.g., TestType, PatientID, etc.), use count() to see their distribution:
lab_record_clean %>%
count(TestType, name = "n_TestType", na = TRUE)
Or for multiple variables:
lab_record_clean %>%
summarise(across(everything(), ~ sum(is.na(.))))
📌 Example Workflow All Together
library(dplyr)
# Basic summary
summary(lab_record_clean)
# Data structure and types
str(lab_record_clean)
# Count missing values per column
colwise_missing <- data.frame(
Missing = colSums(is.na(lab_record_clean)),
row.names = "Variables"
)
print(colwise_missing)
# Count values in key categorical variables
lab_record_clean %>%
count(TestType, name = "n_TestType", na = TRUE)
lab_record_clean %>%
count(year_month, name = "n_YearMonth", na = TRUE)
✅ Interpretation Tips
- If a column has many
NAs, consider whether it's safe to drop or impute.
- Make sure date columns are actually
Date class.
- Ensure numeric columns aren't accidentally stored as characters.
Let me know if you want code tailored to specific column names or to handle missing data further!