在R编程语言中,我想使用哈希表.
如何使用变量的值作为环境的关键?
例如:
map <- new.env(hash=T, parent=emptyenv())
key <- 'ddd'
map$key <- 4
print(ls(map))
>>[1] "key"
Run Code Online (Sandbox Code Playgroud)
输出是'key',这意味着我得到了从字符串'key'到值4的映射.我真正想要这个代码做的是将字符串'ddd'映射到值4.
我怎样才能做到这一点?
PS.我没有使用命名列表,因为它使用大量元素很慢,因为它不使用散列来进行搜索.
Jos*_*ich 30
正如它所说?"$":
Both ‘[[’ and ‘$’ select a single element of the list. The main
difference is that ‘$’ does not allow computed indices, whereas
‘[[’ does. ‘x$name’ is equivalent to ‘x[["name", exact =
FALSE]]’. Also, the partial matching behavior of ‘[[’ can be
controlled using the ‘exact’ argument.
Run Code Online (Sandbox Code Playgroud)
所以你要:
map[[key]] <- 4
> print(ls(map))
[1] "ddd" "key"
> map[[key]]
[1] 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16334 次 |
| 最近记录: |