r - R散列一列不能使用摘要
问题描述
我有一个数据框,其中包含一个作为唯一标识符的列,我想创建一个作为哈希值的新列。我正在使用 dplyr 的转换和摘要进行散列,但看起来所有散列值都是相同的。
status identifier
1 NEW 1035656|8000|157.6|2018-12-10 00:00:00.0|2018-12-06 00:00:00.0
2 NEW 1852231|460000|1748.0|2018-03-09 00:00:00.0|2018-03-07 00:00:00.0
3 NEW 3197282|6000|55.2|2019-01-18 00:00:00.0|2019-01-16 00:00:00.0
4 NEW 1827398|396000|21859.2|2019-02-25 00:00:00.0|2019-02-21 00:00:00.0
5 NEW 1148967|60000|150.0|2018-10-15 00:00:00.0|2018-10-11 00:00:00.0
输出
data_new <- transform(data, hash=digest(identifier, algo="md5", serialize = F))
结果看起来不对。
hash
1 d1ede7da2094651658adfd6171c33c52
2 d1ede7da2094651658adfd6171c33c52
3 d1ede7da2094651658adfd6171c33c52
4 d1ede7da2094651658adfd6171c33c52
5 d1ede7da2094651658adfd6171c33c52
6 d1ede7da2094651658adfd6171c33c52
有人可以向我解释我的语法有什么问题吗?
解决方案
由于哈希算法不在乎您给他多少输入,因此在您的情况下它会压缩您的整个列而不是单个值。该digest
函数旨在散列整个列/列表等。它散列它可以获得的所有内容。因此,为了验证,让我们一次输入您的整个列:
digest( c("1035656|8000|157.6|2018-12-10 00:00:00.0|2018-12-06 00:00:00.0", "1852231|460000|1748.0|2018-03-09 00:00:00.0|2018-03-07 00:00:00.0",
"3197282|6000|55.2|2019-01-18 00:00:00.0|2019-01-16 00:00:00.0", "1827398|396000|21859.2|2019-02-25 00:00:00.0|2019-02-21 00:00:00.0",
"1148967|60000|150.0|2018-10-15 00:00:00.0|2018-10-11 00:00:00.0"), algo="md5", serialize= F)
它给出了你的例子中的输出。由于只有一个返回值,因此该列将填充相同的值。
"d1ede7da2094651658adfd6171c33c52"
解决方案相当简单,只需在列的每一行上使用您的哈希,例如:
df$hash <-lapply(df$identifier, function(x) {digest(x, algo="md5", serialize = F)})
这给出了预期的输出:
identifier hash
1 1035656|8000|157.6|2018-12-10 00:00:00.0|2018-12-06 00:00:00.0 d1ede7da2094651658adfd6171c33c52
2 1852231|460000|1748.0|2018-03-09 00:00:00.0|2018-03-07 00:00:00.0 ca4caeac0a702094d51a13e67f23e56a
3 3197282|6000|55.2|2019-01-18 00:00:00.0|2019-01-16 00:00:00.0 239342dba0ec56f3b4200cb36046f2e0
4 1827398|396000|21859.2|2019-02-25 00:00:00.0|2019-02-21 00:00:00.0 54ea74e4344c14f8708dc47425ee1995
5 1148967|60000|150.0|2018-10-15 00:00:00.0|2018-10-11 00:00:00.0 f6bb25b0d7c1fbb65117d9403dadc7d2
推荐阅读
- php - PHP:在 cURL GET 请求之后立即发出 cURL POST 请求
- javascript - Redux 期望监听器起作用
- c# - 如何调整 Cheet.NET 以绑定到我的 C# 表单?
- sql - BigQuery:检索两列中唯一的行,否则检索第三列最大的行
- sql - 筛选日期范围内退货的项目列表
- windows - Ansible 文件检查多个服务器和文件
- php - 获取并显示 WordPress 上帖子的类别选项
- android - 将反应原生版本更新到 0.59.4 后,未定义不是评估 (_reactnative.Modal.propTypes.animationType) 的对象
- ruby - 如何根据长度将路径数组转换为嵌套数组或散列
- python - 如何显示来自不同模型的对象?