好像dplyr::pull()
和dplyr::select()
做同样的事情。除了dplyr::pull()
仅选择1个变量以外,还有其他区别吗?
您可能会看到或select
的类似物,并且(/ )的类似物或数据框的情况(for列表的类似物是)。[
magrittr::extract
pull
[[
$
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
可以采用literal
,character
或numeric
索引,而其他采用character
或numeric
仅采用
重要的一件事是,它们在处理负索引的方式上有所不同。
对于select
负索引,意味着要减少的列。
因为pull
它们表示从最后一列开始计数。
df %>% pull(-Sepal.Length)
df %>% pull(-1)
# [1] setosa setosa setosa setosa setosa setosa
# Levels: setosa versicolor virginica
奇怪的结果,但Sepal.Length
被转换为1
,和列-1
是Species
(最后一列)
[[
和不支持此功能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] 删除。
我来说两句