简体   繁体   English

dplyr::pull 与 purrr::pluck 和 magrittr::extract2 有何区别?

[英]What distinguishes dplyr::pull from purrr::pluck and magrittr::extract2?

In the past, when working with a data frame and wanting to get a single column as a vector, I would use magrittr::extract2() like this:过去,在处理数据框并希望将单列作为向量时,我会像这样使用magrittr::extract2()

mtcars %>%
  mutate(wt_to_hp = wt/hp) %>%
  extract2('wt_to_hp')

But I've seen that dplyr::pull() and purrr::pluck() also exists to do much the same job: return a single vector from a data frame, not unlike [[ .但是我已经看到dplyr::pull()purrr::pluck()也可以完成相同的工作:从数据框中返回单个向量,与[[不同。

Assuming that I'm always loading all 3 libraries for any project I work on, what are the advantages and use cases of each of these 3 functions?假设我总是为我从事的任何项目加载所有 3 个库,那么这 3 个函数中的每一个的优点和用例是什么? Or more specifically, what distinguishes them from each other?或者更具体地说,它们有什么区别?

When you "should" use a function is really a matter of personal preference.什么时候“应该”使用函数实际上是个人喜好问题。 Which function expresses your intention most clearly.哪个功能最清楚地表达了您的意图。 There are differences between them.它们之间存在差异。 For example, pluck works better when you want to do multiple extractions.例如,当您想要进行多次提取时, pluck效果更好。 From help file:从帮助文件:

 accessor(x[[1]])$foo 
 # is the same as
 pluck(x, 1, accessor, "foo")

so while it can be use to just extract a column, it's useful when you have more deeply nested structures or you want to compose with an accessor function.因此,虽然它可用于仅提取一列,但当您具有更深的嵌套结构或想要使用访问器函数进行组合时,它会很有用。

The pull function is meant to blend in with the result of the dplyr function. pull函数旨在与dplyr函数的结果混合。 It can take the name of a column using any of the ways you can with other functions in the package.它可以使用包中其他函数的任何方式来获取列的名称。 For example it will work with !!例如,它将与!! style expansion where say extract2 will not.样式扩展,其中说extract2不会。

irispull <- function(x) {
  iris %>% pull(!!enquo(x))
}
irispull(Sepal.Length)

And extract2 is nothing more than a "more readable" wrapper for the base function [[ .extract2是基本函数[[的“更具可读性”的包装器。 In fact it's defined as .Primitive("[[") so it expects column names as character or column indexes and integers.事实上,它被定义为.Primitive("[[")所以它期望列名作为字符或列索引和整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM