dplyr :: pull用裸或带引号的字符串作为参数? [英] dplyr::pull with a bare or quoted string as an argument?

查看:80
本文介绍了dplyr :: pull用裸或带引号的字符串作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于阅读 dplyr:的文档:拉并检查示例,我的印象是 var 参数应为裸列名称。例如:

Based on reading the documentation of dplyr::pull and examining the examples, I am under the impression that the var argument should be a "bare column name". For example:

dplyr::pull(mtcars, cyl)
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

但是,我刚刚意识到带引号的列名也可以使用:

However, I just realized that a quoted column name works too:

dplyr::pull(mtcars, "cyl")
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

我只是好奇这两种样式之间是否存在差异。此外,使用第二种方法进行非交互用法(例如R程序包开发)有任何不利之处。

I am just curious as to whether there are any differences between these two styles. Moreover, are there any downsides of using the second option for say non-interactive usage (e.g. R package development).

我在这些方法中使用的是dplyr 0.7.4例子。

I have using version 0.7.4 of dplyr in these examples.

谢谢

推荐答案

pull 调用 select_var 使用准引用来计算或不计算参数,并最终从数据中返回列名。这样可以灵活地指定列,以支持交互和编程使用。

pull calls select_var which uses quasiquotation to evaluate or not the argument and eventually return a column name from the data. This allows the column to be specified in a flexible way that supports both interactive and programming use.

a <- "cyl"
select_var(names(mtcars), a)
[1] "cyl"
pull(mtcars,a)
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

这篇关于dplyr :: pull用裸或带引号的字符串作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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