dplyr 中的 pull 和 select 之间的区别? [英] Difference between pull and select in dplyr?

查看:22
本文介绍了dplyr 中的 pull 和 select 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来 dplyr::pull()dplyr::select() 做同样的事情.除了 dplyr::pull() 只选择 1 个变量之外还有什么区别吗?

It seems like dplyr::pull() and dplyr::select() do the same thing. Is there a difference besides that dplyr::pull() only selects 1 variable?

推荐答案

您可以将 select 视为 [magrittr::extractpull 作为数据帧的 [[(或 $)或 magrittr::extract2 的类似物(列表中 [[ 的类似物是 purr::pluck).

You could see select as an analogue of [ or magrittr::extract and pull as an analogue of [[ (or $) or magrittr::extract2 for data frames (an analogue of [[ for lists would be purr::pluck).

df <- iris %>% head

所有这些都给出相同的输出:

All of these give the same output:

df %>% pull(Sepal.Length)
df %>% pull("Sepal.Length")
a <- "Sepal.Length"; df %>% pull(!!quo(a))
df %>% extract2("Sepal.Length")
df %>% `[[`("Sepal.Length")
df[["Sepal.Length"]]

# all of them:
# [1] 5.1 4.9 4.7 4.6 5.0 5.4

所有这些都给出相同的输出:

And all of these give the same output:

df %>% select(Sepal.Length)
a <- "Sepal.Length"; df %>% select(!!quo(a))
df %>% select("Sepal.Length")
df %>% extract("Sepal.Length")
df %>% `[`("Sepal.Length")
df["Sepal.Length"]
# all of them:
#   Sepal.Length
# 1          5.1
# 2          4.9
# 3          4.7
# 4          4.6
# 5          5.0
# 6          5.4

pullselect 可以采用 literalcharacternumeric 索引, 而其他人只接受 characternumeric

pull and select can take literal, character, or numeric indices, while the others take character or numeric only

一件重要的事情是它们在处理负指数的方式上有所不同.

One important thing is they differ on how they handle negative indices.

对于 select 负索引意味着要删除的列.

For select negative indices mean columns to drop.

对于pull,它们的意思是从最后一列开始计数.

For pull they mean count from last column.

df %>% pull(-Sepal.Length)
df %>% pull(-1)
# [1] setosa setosa setosa setosa setosa setosa
# Levels: setosa versicolor virginica

奇怪的结果但是Sepal.Length被转换为1,并且列-1Species(最后列)

Strange result but Sepal.Length is converted to 1, and column -1 is Species (last column)

[[extract2 不支持此功能:

This feature is not supported by [[ and extract2 :

df %>% `[[`(-1)
df %>% extract2(-1)
df[[-1]]
# Error in .subset2(x, i, exact = exact) : 
#   attempt to select more than one element in get1index <real>

[extract 支持删除列的负索引.

Negative indices to drop columns are supported by [ and extract though.

df %>% select(-Sepal.Length)
df %>% select(-1)
df %>% `[`(-1)
df[-1]

#   Sepal.Width Petal.Length Petal.Width Species
# 1         3.5          1.4         0.2  setosa
# 2         3.0          1.4         0.2  setosa
# 3         3.2          1.3         0.2  setosa
# 4         3.1          1.5         0.2  setosa
# 5         3.6          1.4         0.2  setosa
# 6         3.9          1.7         0.4  setosa

这篇关于dplyr 中的 pull 和 select 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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