Open bedantaguru opened 4 years ago
Here is the prototype
require(magrittr) #> Loading required package: magrittr select.df <- function(.x, ...) { pos <- tidyselect::eval_select(rlang::expr(c(...)), .x) rlang::set_names(.x[pos], names(pos)) } rename.df <- function(.x, ...) { pos <- tidyselect::eval_rename(rlang::expr(c(...)), .x) names(.x)[pos] <- names(pos) .x } col_contains <- function(str, dat = tidyselect::peek_data(fn = "col_contains"), .ignore_case = T) { if(.ignore_case){ str <- tolower(str) dat %>% purrr::map_lgl(~.x %>% tolower() %>% stringr::str_detect(str) %>% any) %>% colnames(dat)[.] }else{ dat %>% purrr::map_lgl(~stringr::str_detect(.x, str) %>% any) %>% colnames(dat)[.] } } d <- iris class(d)<- c( "df", class(d)) d %>% dplyr::rename(tst = col_contains("seto")) %>% head() #> Sepal.Length Sepal.Width Petal.Length Petal.Width tst #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa d %>% dplyr::select(col_contains("ginica")) %>% head() #> Species #> 1 setosa #> 2 setosa #> 3 setosa #> 4 setosa #> 5 setosa #> 6 setosa
Created on 2020-04-03 by the reprex package (v0.3.0)
This requires all output objects to be marked as {df} class (whichever are data.frames)
for {df} class create rename method with snake_case and camelCase options..
{df} class
snake_case
camelCase
Here is the prototype
Created on 2020-04-03 by the reprex package (v0.3.0)