从mpfr(R)生成的列表中获取值

时间:2017-12-28 14:10:31

标签: r list

我正在尝试以下部分代码。我得到0到1之间的数字,我需要进行其余的计算。

V<-mpfr("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", base=16)

H <- rep(0, 10)

for (i in 1:10) {
   H[i]= mpfr(digest(i, algo="sha256", serialize=TRUE), base=16)/V
}
print(H)

所以我的问题是打印清单H后的问题:

[[1]]
'mpfr1' 0.3755394259369469231611897454479156306810504715123336532380340752704582135787074

[[2]]
'mpfr1' 0.6551563805108653170634236534090611547455620826606370088028477863221605874721831
...

所以我尝试了以下内容:

print(H[[1]][2])
Error in H[[1]][2] : object of type 'S4' is not subsettable

如何只访问那些浮点数(没有'mpfr1')?我尝试了几个技巧并阅读了这个包的pdf但没有成功。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

library(Rmpfr)

V <- mpfr("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", base=16)

# idiomatic way to pre-allocate list storage
H <- vector("list", 10)

问题在于使用[ vs [[

for (i in 1:10) {
  H[[i]] = mpfr(digest::digest(i, algo="sha256", serialize=TRUE), base=16)/V
}

打印整件事:

print(H)
## [[1]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.3755394259369469231611897454479156306810504715123336532380340752704582135787074
## 
## [[2]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.6551563805108653170634236534090611547455620826606370088028477863221605874721831
## 
## [[3]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.3885949915720266863548135792781703111939691431200061007408307773606977937809833
## 
## [[4]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.3150499099587520139267833566690324328646190056708170872163816526136404907018551
## 
## [[5]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.6960615309583028424977971735890982334238399076523490261098745534587580595780822
## 
## [[6]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.5106268197476398863646910767785345221093749399244235093877311378854347607479013
## 
## [[7]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.6797735938098921024956339510265110382807465503448772685158966413891520889142766
## 
## [[8]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.7664394982397497402230927479011597236790782567007390733811624944306336973288917
## 
## [[9]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.2894251888739856297974876999320599099412274126754843872782469572239637764268605
## 
## [[10]]
## 1 'mpfr' number of precision  256   bits 
## [1] 0.1950676091763967641866892764916813427538223237940125721449757977662242162599494

使用个别元素:

as.numeric(H[[1]])
## [1] 0.3755394