在R环境中如何迭代哈希项? [英] How to iterate through hash items, in an R environment?
本文介绍了在R环境中如何迭代哈希项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但是如何遍历环境中的所有项目?
当我运行以下代码时,我期望输出如下:
1
2
但是,我得到两行NULL,我该如何得到我想要的?
map< - new.env(hash = T,parent = emptyenv())
assign('a ',1,map)
assign('b',2,map)
for(v in ls(map)){
print(map $ v)
}
解决方案
在一个函数内使用$希望解释输入是编程错误的常见来源。使用form对象[[value]](不带引号。)
for(v in ls(map)){
print(map [[v]])
}
I'm trying to find a way to use a hash map in R, and after some searching I get the R-environment. But how can I iterate through all the items in an environment ? When I run the following code, I was expecting output like this :
1
2
But I get two lines of NULL instead, how can I get what I want ?
map <- new.env(hash=T, parent=emptyenv())
assign('a', 1, map)
assign('b', 2, map)
for (v in ls(map)) {
print(map$v)
}
解决方案
The use of "$" inside a function where it is desired to interpret the input is a common source of programming error. Use instead the form object[[value]] (without the quotes.)
for (v in ls(map)) {
print(map[[v]])
}
这篇关于在R环境中如何迭代哈希项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文