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

查看:312
本文介绍了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 ::提取 pull 作为 [[(或 $ )或 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

pull select 可以使用文字字符数字索引,而其他则采用字符或仅数字

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.

对于选择负索引意味着要删除的列。

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 ,列 -1 Species (最后一列)

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

[[ 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>

[和提取

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天全站免登陆