Suggest function to assert column name

Issue #15 resolved
Former user created an issue

Thank you very much for a very good and useful package that I've started to use after your tutorial in Aalborg!

One function I find handy is to check wether a string matches a column name in a data.frame. I now use this function:

assert_is_col_name <- function(var, df) { assertive::assert_is_a_string(var) assertive::assert_is_of_length(var, 1) assertive::assert_is_subset(var, names(df)) }

Maybe that could be something to consider for the package?

Comments (3)

  1. Former user Account Deleted
    assert_is_col_name <- function(var, df) {
      assertive::assert_is_a_string(var)
      assertive::assert_is_of_length(var, 1)
      assertive::assert_is_subset(var, names(df))
    }
    
  2. Richard Cotton repo owner

    Hi Erik,

    I'm glad that the package is useful for you. Note that assert_is_a_string already contains a check for the input having length 1 (it combines assert_is_character and assert_is_scalar).

    I agree that using is_subset is a good way to check for the presence of columns. I'm not convinced that a whole new function is needed, but I've added this use case to example(is_subset).

    Thanks for the feedback and idea.

  3. Log in to comment