非标准评估和字符向量 [英] Non-Standard Evaluation and Character Vectors

查看:63
本文介绍了非标准评估和字符向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个如下的数据集

Imagine I have a dataset as follows

d <- data.frame(a = 1:10, b = letters[1:10], c = sample(10))

想象一下,我也有一个包含变量名的字符向量

Imagine that I also have a character vector containing variable names

v <- c("a", "b")

使用 dplyr 我想使用 v 选择变量 a & b .

Using dplyr I would like to use v to select variables a & b.

这行不通,

d %>%
  select(v)

这是因为 dplyr 软件包使用非标准评估,并且希望传递实际的变量名而不是字符.

This is because the dplyr packages uses non standard evaluation and expects actual variable names to be passed rather then characters.

推荐答案

对于标准评估,您需要在给定名称后使用带下划线的功能.在这种情况下,它是 select _().而且我们还需要使用 .dots 参数将向量插入到调用中.

For standard evaluation, you will want to use the functions with an underscore after their given name. In this case that is select_(). And we will also need to use the .dots argument to insert your vector into the call.

d %>% select_(.dots = v)

有关更多信息,请参见 help(select) vignette("nse").

See help(select) and vignette("nse") for more.

这篇关于非标准评估和字符向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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