## 3.4 Combining “Validity” Flags

It is often desirable to combine all deletion criteria into a single binary variable. This means combining multiple binary variables, returning `FALSE` if any of the values are `FALSE`, but only returning `TRUE` if all of the values are `TRUE`.

``````CD166_19_xrf <- CD166_19_xrf %>%
mutate(slope = `sample surface` - dplyr::lag(`sample surface`)) %>%
mutate(in_slope_tolerance = ifelse(slope <=-0.1 | slope >=0.1 | is.na(slope) == TRUE, FALSE, TRUE)) %>%
select(-slope) %>%
mutate(in_cps_tolerance = ifelse(cps <=30000 | cps >=60000 | is.na(cps) == TRUE, FALSE, TRUE)) %>%
mutate(in_mse_tolerance = ifelse(MSE <=2, TRUE, FALSE)) %>%
rowwise() %>%
mutate(qc = !any(c(validity, in_slope_tolerance, in_cps_tolerance, in_mse_tolerance) == FALSE)) %>%
ungroup() %>%
select(-c(in_slope_tolerance, in_cps_tolerance, in_mse_tolerance)) #%>%

ggplot(data = CD166_19_xrf, aes(y = depth, x = `Mo inc`/`Mo coh`, col = qc)) +
geom_lineh(aes(group = 1)) +
geom_rug(sides = "l", data = . %>% filter(qc == FALSE)) +
scale_color_discrete(name = "pass QC") +
scale_y_reverse(name = "depth [mm]")`````` Bear in mind that this doesn’t remove observations considered defective, only marks them has `qc == FALSE`. If they are to be excluded from subsequent analysis, they should be removed using `filter(qc == TRUE)`.