首页 文章

Dplyr mutate:使得vector之间的差异成为元素,而vector将作为vector

提问于
浏览
2

我正在使用dplyr来操纵数据 . 我有两列:x和y . 在第三列(比如z)中,我想在所有x列中得到y的第一个索引 .

例如:

enter image description here

对于第一行,我得到4,因为7位于x的第4位 .

所以我试过了

df <- df %>% 
  mutate(z = which (x==y)[1])

但是比较是按元素进行的(即我在z中只得到五个) . 因此,我的问题是:如何在矢量中采用元素智能和矢量在dplyr mutate中作为矢量?

1 回答

  • 1

    dplyr 不决定该功能是否以元素方式应用 . mutate 仅提供一种语法,通过识别如果在 mutate 中引用 x ,您可以更简洁地使用其他函数,您可能意味着 df 中的列 df$x . 它还执行一个简单的广播步骤,如果您为其提供仅返回单个值的函数,则会将其复制到整个输出 .

    我们可以在下面的dplyr之外的 whichmatch 中显示相同的行为 . 因为 == 进行元素比较,所以第一个方法返回所有 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
    

    reprex package(v0.2.0)创建于2018-06-29 .

相关问题