Hey there,
I have been trying a few ways to create a custom function that takes a tibble and drops the columns named in the function. I know that for select commands you can pass the dots and select those columns, but how to unselect them?
requires(rlang)
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
df_output <-
df %>%
select(-g1, -g2)
my_select <- function(df, ...) {
group_sel <- enquos(...)
df %>%
select(!!!group_sel)
}
my_select(df, g1, g2)
# Works like a charm
my_second_select <- function(df, ...) {
df %>%
select(...)
}
my_second_select(df, g1, g2)
# Works too
## But for unselecting
my_unselect1 <- function(df, ...) {
group_sel <- enquos(...)
df %>%
select(-!!!group_sel)
}
my_unselect1(df, g1,g2)
#NOPE
my_unselect2 <- function(df, ...) {
df %>%
select(-...)
}
my_unselect2(df, g1,g2)
# NOPE
my_unselect3 <- function(df, ...) {
sel <- tidyselect::vars_select(tbl_vars(.data), ...)
df %>%
select(-!!!sel)
}
my_unselect3(df, g1,g2)
#NOPE
Any clues on how to do this? It seems like it should be super easy, but I can't get my head around it