Excluding column from gather() from tidyr package using standard evaluation

拜拜、爱过 提交于 2020-01-24 07:26:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!