在R中使用dplyr和摘要向每行添加哈希 [英] adding hash to each row using dplyr and digest in R
问题描述
我需要向数据集中的每一行添加一个指纹,以便与同一组的更高版本进行检查以寻找差异.
I need to add a fingerprint to each row in a dataset so to check with a later version of the same set to look for difference.
我知道如何为R中的每一行添加哈希,如下所示:
I know how to add hash for each row in R like below:
data.frame(iris,hash=apply(iris,1,digest))
我正在学习使用 dplyr
,因为数据集越来越大,我需要将它们存储在SQL Server中,我尝试了如下所示的操作,但是散列不起作用,所有行都给出相同的散列:
I am learning to use dplyr
as the dataset is getting huge and I need to store them in SQL Server, I tried something like below but the hash is not working, all rows give the same hash:
iris %>%
rowwise() %>%
mutate(hash=digest(.))
使用dplyr进行行哈希的任何线索吗?谢谢!
Any clue for row-wise hashing using dplyr? Thanks!
推荐答案
我们可以使用 do
res <- iris %>%
rowwise() %>%
do(data.frame(., hash = digest(.)))
head(res, 3)
# A tibble: 3 x 6
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species hash
# <dbl> <dbl> <dbl> <dbl> <fctr> <chr>
#1 5.1 3.5 1.4 0.2 setosa e261621c90a9887a85d70aa460127c78
#2 4.9 3.0 1.4 0.2 setosa 7bf67322858048d82e19adb6399ef7a4
#3 4.7 3.2 1.3 0.2 setosa c20f3ee03573aed5929940a29e07a8bb
请注意,在 apply
过程中,所有列都将转换为单个类,因为 apply
转换为 matrix
,矩阵只能容纳一个班级.有关将 factor
转换为 character
类
Note that in the apply
procedure, all the columns are converted to a single class as apply
converts to matrix
and matrix can hold only a single class. There will be a warning about converting the factor
to character
class
这篇关于在R中使用dplyr和摘要向每行添加哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!