I am looking for a "tidy" and concise way to make sure that a combination of columns is unique in a tibble using assertr
.
So far, this is the best I could come up with:
PasteRows <- function(df) {
apply(df, 1, paste, collapse = '')
}
tib <- tibble(a = c(1, 1, 3), b = c('a', 'b', 'b'))
tib %>%
assert_rows(PasteRows, is_uniq, a, b)
... but I first have to define PasteRows
. Also, I am not sure if apply
has a performance penalty, because it converts the tibble into a matrix.
How can I improve and shorten this?
assertr
has the function col_concat()
for this purpose.
library(assertr)
library(tibble)
tib <- tibble(a = c(1, 1, 2), b = c('a', 'b', 'b'))
tib %>%
assert_rows(col_concat, is_uniq, a, b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With