dplyr中pull和select之间的区别?

埃文·O。

好像dplyr::pull()dplyr::select()做同样的事情。除了dplyr::pull()仅选择1个变量以外,还有其他区别吗?

Moody_Mudskipper

您可能会看到select的类似物,并且(/ 的类似物数据框的情况(for列表的类似物)。[magrittr::extractpull[[$magrittr::extract2[[purr::pluck

df <- iris %>% head

所有这些都提供相同的输出:

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

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

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可以采用literalcharacternumeric索引,而其他采用characternumeric仅采用

重要的一件事是,它们在处理负索引的方式上有所不同。

对于select负索引,意味着要减少的列。

因为pull它们表示从最后一列开始计数。

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

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

[[不支持此功能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尽管支持负列删除索引

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章