student_fruit_preferences <- data.frame(
student_id = c(1:5, NA, NA),
apple = c(1, 1, 1, 1, 99, NA, NA),
orange = c(2, 3, 2, 3, 99, NA, NA),
banana = c(3, 2, 3, 2, 99, NA, NA),
phone1 = c(123, 456, 789, 987, 654, NA, NA),
phone2 = c(345, 678, 987, 567, 000, NA, NA)
)
# Check that key is unique, excluding NAs by default
expect_unique(student_id, data = student_fruit_preferences)
# Check that key is unique, including NAs
try(expect_unique(student_id, exclude = NULL, data = student_fruit_preferences))
# Check each fruit has unique preference number
try(
expect_unique_across(
c(apple, orange, banana),
data = student_fruit_preferences
)
)
# Check each fruit has unique preference number, allowing multiple 99 (item
# skipped) codes
expect_unique_across(
c(apple, orange, banana),
exclude = c(99, NA), data = student_fruit_preferences
)
# Check that each phone number appears at most once
try(expect_unique_combine(c(phone1, phone2), data = student_fruit_preferences))
Run the code above in your browser using DataLab