Why does the digest function return the same value every time when used with dplyr's mutate?

前端 未结 1 1195
死守一世寂寞
死守一世寂寞 2021-01-12 04:13

Here\'s a data frame containing a column of user ids:

> head(df)
       uid
1 14070210
2 14080815
3 14091420

For the sake of argument, I

相关标签:
1条回答
  • 2021-01-12 04:35

    The digest() function isn't vectorized. So if you pass in a vector, you get one value for the whole vector rather than a digest for each element of the vector. Since it returns one value, that value is recycled for each row of your data.frame. You can create your own vectorized version

    vdigest <- Vectorize(digest)
    df %>% mutate(sqrt_uid = sqrt(uid), hashed_uid = vdigest(uid))
    #        uid sqrt_uid                       hashed_uid
    # 1 14070210 3751.028 cc90019421220a24f75b5ed5daec36ff
    # 2 14080815 3752.441 9f7f643940b692dd9c7effad439547e8
    # 3 14091420 3753.854 89e6666fdfdbfb532b2d7940def9d47d
    

    which matches what you get when you pass in each vector element individually

    digest(df$uid[1])
    # [1] "cc90019421220a24f75b5ed5daec36ff"
    digest(df$uid[3])
    # [1] "89e6666fdfdbfb532b2d7940def9d47d"
    
    0 讨论(0)
提交回复
热议问题