Open maurolepore opened 6 years ago
Original idea by Luis Verde
https://rstudio-pubs-static.s3.amazonaws.com/287966_3967f466282b4260a0163d9d9acdad57.html
library(dplyr, warn.conflicts = FALSE) library(tidyr) library(rlang) (dat <- tibble( jumble = c("Muridae", "diet:seeds", "Sp1", "Sp2", "Sp3", "diet:unknown", "Sp4", "Sp5", "Cricetidae", "diet:fruits", "Sp11", "Sp32", "Sp113") )) #> # A tibble: 13 x 1 #> jumble #> <chr> #> 1 Muridae #> 2 diet:seeds #> 3 Sp1 #> 4 Sp2 #> 5 Sp3 #> 6 diet:unknown #> 7 Sp4 #> 8 Sp5 #> 9 Cricetidae #> 10 diet:fruits #> 11 Sp11 #> 12 Sp32 #> 13 Sp113 untangle2 <- function(df, regex, orig, new) { orig <- enquo(orig) new <- sym(quo_name(enquo(new))) df %>% mutate( !!new := if_else(grepl(regex, !! orig), !! orig, NA_character_) ) %>% fill(!! new) %>% filter(!grepl(regex, !! orig)) } dat %>% untangle2("dae$", jumble, family) %>% untangle2("^diet", jumble, diet) #> # A tibble: 8 x 3 #> jumble family diet #> <chr> <chr> <chr> #> 1 Sp1 Muridae diet:seeds #> 2 Sp2 Muridae diet:seeds #> 3 Sp3 Muridae diet:seeds #> 4 Sp4 Muridae diet:unknown #> 5 Sp5 Muridae diet:unknown #> 6 Sp11 Cricetidae diet:fruits #> 7 Sp32 Cricetidae diet:fruits #> 8 Sp113 Cricetidae diet:fruits
Created on 2018-10-06 by the reprex package (v0.2.1)
Original idea by Luis Verde
https://rstudio-pubs-static.s3.amazonaws.com/287966_3967f466282b4260a0163d9d9acdad57.html
Created on 2018-10-06 by the reprex package (v0.2.1)