0

I'm trying to drop columns from the columns to be gathered using the gather() function in the R package tidyr.

This works fine when I specify what columns I want to gather (here, I'm gathering the mpg, cyl, and disp columns into variable and val columns:

library(tidyverse)

mtcars %>% 
  select_("mpg", "cyl", "disp") %>% 
  gather_("variable", "val", gather_cols = c("mpg", "cyl", "disp"))

Let's say I had four variables, and instead of specifying which to gather, I wanted to specify which to drop (hp in this next case). This, probably obviously, doesn't work:

mtcars %>% 
  select_("mpg", "cyl", "disp", "hp") %>% 
  gather_("var", "val", gather_cols = c(-"hp"))

The suggestion to use one_of - as in the answer to this question - doesn't seem to work. In a now-closed GitHub issue for tidyr, it is suggested to use dplyr::select_vars(), but this doesn't seem to work, either.

How could this - specifying which columns to drop with standard evaluation using gather()?

Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
  • `mtcars %>% gather_("k", "v", colnames(.)[!colnames(.) %in% c("hp", "am")])`? – Nate Jul 09 '17 at 00:09
  • Thanks! This works; I changed to this to parallel the example above: `mtcars %>% select_("mpg", "cyl", "disp", "hp") %>% gather_("variable", "val", colnames(.)[!colnames(.) %in% c("hp")])` boy, this is verbose and not easy to understand, though. – Joshua Rosenberg Jul 09 '17 at 01:08

2 Answers2

0

I'm a bit unfamiliar with what tidyverse loads and how it manages environments -- I usually use regular tidyr -- but using tidyr::gather instead of gather_, would allow you to use this syntax:

# here, gather takes the "key" and "value" args as objects
# as does dplyr::select
# and allows for additional objects off the data.frame
# to be passed as additional args

mtcars %>% 
  dplyr::select(mpg, cyl, disp, hp) %>% 
  tidyr::gather(var, val, -hp)
cmaher
  • 5,100
  • 1
  • 22
  • 34
  • Thanks. library(tidyverse) loads dplyr and tidyr (and a few other packages). In this case at least, the code above should work the same were each of the packages loaded on their own. I'm sorry if I wasn't clear - but I need to use standard evaluation (variables names are quoted - unlike how select() and gather() usually work). It's because I'm using this in a function and have to pass the variables as character strings. – Joshua Rosenberg Jul 09 '17 at 00:04
0

What about this?

mtcars %>% 
    select_("mpg", "cyl", "disp", "hp") %>% 
    gather_("var", "val", select_vars(include = c('mpg', 'cyl', 'disp')))

or if you want to specify the excluded column:

mtcars %>% 
  select_("mpg", "cyl", "disp", "hp") %>% 
  gather(var, val, select_vars(include = -matches('hp')))
lhughes
  • 1
  • 1