R (tidyverse)+
library(tidyverse)
# -- Sample dataset with NAs scattered through ---------------------------------
demo <- tribble(
~subj_id, ~Sex, ~Age, ~Height, ~Weight, ~baseline_score, ~visit_score,
"S001", "M", 14, 69.0, 112.5, NA, 78,
"S002", "F", 13, 56.5, NA, 80, 88,
"S003", NA, 13, 65.3, 98.0, 73, NA,
"S004", "F", 14, NA, 102.5, 88, 85,
"S005", "M", NA, 63.5, 102.5, 79, NA,
"S006", "M", 12, 57.3, 83.0, NA, NA
)- We build
demowithtribble()so the NA pattern is visible at a glance — Sex is missing for S003, Age for S005, Height for S004, Weight for S002, baseline_score for S001 and S006, visit_score for S003, S005, S006. - Almost every scenario below is a pipe starting from
demo(or from a small reference tibble where a data-frame form is artificial) and returns a tibble assigned toscenarioNN. Two lines are deliberately bare — thefilter(Sex == NA)trap in scenario05 and the no-na.rmsummarise(mean_weight = mean(Weight))in scenario06 — placed right before the correct version so the contrast is visible.