问题
I'm trying to exclude a column from gather()
from the tidyr package using standard evaluation.
Using non-standard evaluation, this works fine:
mtcars_df <- head(mtcars[, c("mpg", "hp", "cyl")])
tidyr::gather(mtcars_df, key, val, -cyl)
However, using standard evaluation, neither of the following work and both return an error:
tidyr::gather_(mtcars_df, "key", "val", -"cyl")
tidyr::gather_(mtcars_df, "key", "val", -cyl)
As a work-around, I tried to use the select()
helper function contains()
, but this just returned the original data.frame
, mtcars_df
:
tidyr::gather_(mtcars_df, "key", "val", -dplyr::contains("cyl"))
How can I exclude a column in this way using standard evaluation? What's going on here?
回答1:
According to tidyr github issue #109: gather_ no longer supports dropping a column?,
gather_() only works with character vectors. - Hadley Wickham, 2015
You can try
library(dplyr)
mtcars_df %>% tidyr::gather_("key", "val", setdiff(names(.), "cyl"))
回答2:
Altough your workaround works, it creates a NOTE
when used in a package:
no visible binding for global variable '.'
Which prevents it from being published on CRAN :(
Is there any other way to exclude column with gather_
using standart evaluation?
I've tried many things (such as using dplyr::select_vars
) but nothing worked...
来源:https://stackoverflow.com/questions/42285006/excluding-column-from-gather-from-tidyr-package-using-standard-evaluation