使用标准评估从 tidyr 包中从 Gather() 中排除列 [英] Excluding column from gather() from tidyr package using standard evaluation

查看:14
本文介绍了使用标准评估从 tidyr 包中从 Gather() 中排除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用标准评估从 tidyr 包中排除 gather() 中的列.

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)

作为一种解决方法,我尝试使用 select() 辅助函数 contains(),但这只是返回了原始的 data.frame, mtcars_df:

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?

推荐答案

根据 tidyr github issue #109:gather_ 不再支持删除列?

According to tidyr github issue #109: gather_ no longer supports dropping a column?,

gather_() 仅适用于字符向量.- 哈德利·威克姆,2015 年

gather_() only works with character vectors. - Hadley Wickham, 2015

你可以试试

library(dplyr)
mtcars_df %>% tidyr::gather_("key", "val", setdiff(names(.), "cyl"))

这篇关于使用标准评估从 tidyr 包中从 Gather() 中排除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆