我们可以在下面的dplyr之外的 which 和 match 中显示相同的行为 . 因为 == 进行元素比较,所以第一个方法返回所有 5 . match 另一方面,"returns a vector of the positions of (first) matches of its first argument in its second"(来自文档)这是你想要的 . 我比较了底部的两个语法,以表明密钥是您提供的函数,它确定如何读取输入,而不是 mutate .
x = c(1,2,3,7,9)
y = c(7,3,9,1,9)
x == y
#> [1] FALSE FALSE FALSE FALSE TRUE
which(x == y)
#> [1] 5
match(y, x)
#> [1] 4 3 5 1 5
library(dplyr)
df <- data.frame(x, y)
df$z1 = match(df$y, df$x) # a base R syntax that forces you to specify the data frame name
df <- df %>% mutate(z2 = match(y, x)) # dplyr syntax that is more concise
df # they produce the same result
#> x y z1 z2
#> 1 1 7 4 4
#> 2 2 3 3 3
#> 3 3 9 5 5
#> 4 7 1 1 1
#> 5 9 9 5 5
1 回答
dplyr
不决定该功能是否以元素方式应用 .mutate
仅提供一种语法,通过识别如果在mutate
中引用x
,您可以更简洁地使用其他函数,您可能意味着df
中的列df$x
. 它还执行一个简单的广播步骤,如果您为其提供仅返回单个值的函数,则会将其复制到整个输出 .我们可以在下面的dplyr之外的
which
和match
中显示相同的行为 . 因为==
进行元素比较,所以第一个方法返回所有5
.match
另一方面,"returns a vector of the positions of (first) matches of its first argument in its second"(来自文档)这是你想要的 . 我比较了底部的两个语法,以表明密钥是您提供的函数,它确定如何读取输入,而不是mutate
.由reprex package(v0.2.0)创建于2018-06-29 .