Use na.omit() to remove observations containing missing values before analysis. It returns a cleaned object while leaving the original data set unchanged when applied to a temporary subset. section 10.4.2
A practical missing-data workflow is:
- Select only the variables needed for the next plot or calculation. section 10.4.2
- Assign the subset to a temporary object, then call na.omit() on that object. section 10.4.2
- Pass the resulting complete observations to plotting, correlation, or summary functions. section 10.4.1 section 10.4.3
Prepare NBA payroll data for a plot
Create a focused data frame and remove incomplete rows without altering salaries: salaries2000 <- select(salaries, Team, s2000, pc2000); salaries2000 <- na.omit(salaries2000). The cleaned object can then support a payroll dot plot grouped by playoff outcome. section 10.4.2
salaries2000 <- select(salaries, Team, s2000, pc2000)
salaries2000 <- na.omit(salaries2000)
dot1 <- ggplot(salaries2000) +
geom_point(aes(x = s2000, y = reorder(Team, s2000),
color = factor(pc2000)), size = 3) +
labs(title= "NBA Team Payrolls (USD)",
subtitle = "1999-2000",
x = "Team Payroll",
y = "") +
scale_x_continuous(labels = label_dollar(scale_cut = cut_short_scale())) +
theme(plot.title = element_text(face = "bold")) +
scale_color_manual(name = "",
labels = c("No playoffs",
"Made playoffs",
"Won title"),
values = c("0" = "navy",
"10" = "gold3",
"11" = "red")) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Browse the complete R listing for selecting columns, removing missing rows, and building the 1999–2000 payroll visualization. section 10.4.2
