diff --git a/misc.R b/misc.R index 5d016e3..b0b765d 100644 --- a/misc.R +++ b/misc.R @@ -7,14 +7,31 @@ library(plotly) library(forcats) library(markdown) -uniq <- function(df, ...) +uniq <- function(df, ..., first = F) { + uniq_vector <- function(x, fst) + { + x %>% + c %>% + rle %>% + .$lengths -> + l + + if (fst) + { + l %>% + cumsum %>% + `-`(l) + 1 + } else + { + l %>% cumsum + } + } + eval(substitute(alist(...))) %>% map_chr(as.character) %>% intersect(names(df)) %>% - map(~rle(df[[.x]] %>% c) %>% - .$lengths %>% - cumsum) %>% + map(~uniq_vector(df[[.x]], first)) %>% reduce(union) %>% sort -> columns