Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to add {col_contains} column selector, which is based on content of the column #29

Open
bedantaguru opened this issue Apr 3, 2020 · 2 comments

Comments

@bedantaguru
Copy link
Member

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)

@bedantaguru
Copy link
Member Author

This requires all output objects to be marked as {df} class (whichever are data.frames)

@bedantaguru
Copy link
Member Author

for {df} class create rename method
with snake_case and camelCase options..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant