给定一个字符串/文件名在R中产生一个哈希? [关闭]

问题描述 投票:-3回答:2

我有一组文件名:

f <- c("a", "b", "c") 

我已经找到了digest功能来做这个,你能告诉我是否选择了正确的任务功能?

r string hash
2个回答
0
投票

我们也可以使用for循环

for(i in f) print(digest(i, serialize = FALSE))
#[1] "0cc175b9c0f1b6a831c399e269772661"
#[1] "92eb5ffee6ae2fec3ad71c777531578f"
#[1] "4a8a08f09d37b73795649038408b5f33"

或者使用Vectorize

Vectorize(function(x) digest(x, serialize = FALSE))(f)
#                                a                                  b 
"0cc175b9c0f1b6a831c399e269772661" "92eb5ffee6ae2fec3ad71c777531578f" 
#                                 c 
#"4a8a08f09d37b73795649038408b5f33" 

1
投票

Maby sapply可以为你工作

library(digest)
f <- c("a", "b", "c")
sapply(f, digest, algo="md5", serialize=F)

结果是:

               a                                  b                                  c 
"0cc175b9c0f1b6a831c399e269772661" "92eb5ffee6ae2fec3ad71c777531578f" "4a8a08f09d37b73795649038408b5f33" 
© www.soinside.com 2019 - 2024. All rights reserved.