From f1826b18ea6b5d1951cc9d22a76f72de43e16a15 Mon Sep 17 00:00:00 2001 From: Maxime Wack Date: Thu, 27 Jul 2017 07:57:53 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20param=C3=A8tre=20"first"=20dans=20la=20?= =?UTF-8?q?fonction=20uniq()=20pour=20prendre=20la=20premi=C3=A8re=20occur?= =?UTF-8?q?rence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misc.R | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) 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