R中有字典功能吗

发布于 2024-12-10 11:19:06 字数 545 浏览 0 评论 0 原文

有没有办法在 R 中创建一个“字典”,使其具有对? 大意是:

x=dictionary(c("Hi","Why","water") , c(1,5,4))
x["Why"]=5

我问这个是因为我实际上正在寻找两个类别变量函数。

因此,如果 x=dictionary(c("a","b"),c(5,2))

     x  val
1    a  5 
2    b  2 

我想在 x 键的所有组合上计算 x1^2+x2

     x1 x2 val1  val2  x1^2+x2
1    a  a   5     5      30
2    b  a   2     5      9
3    a  b   5     2      27
4    b  b   2     2      6

然后我希望能够检索使用 x1 和 x2 得到的结果。一些效果: get_result["b","a"] = 9

最好、最有效的方法是什么?

Is there a way to create a "dictionary" in R, such that it has pairs?
Something to the effect of:

x=dictionary(c("Hi","Why","water") , c(1,5,4))
x["Why"]=5

I'm asking this because I am actually looking for a two categorial variables function.

So that if x=dictionary(c("a","b"),c(5,2))

     x  val
1    a  5 
2    b  2 

I want to compute x1^2+x2 on all combinations of x keys

     x1 x2 val1  val2  x1^2+x2
1    a  a   5     5      30
2    b  a   2     5      9
3    a  b   5     2      27
4    b  b   2     2      6

And then I want to be able to retrieve the result using x1 and x2. Something to the effect of:
get_result["b","a"] = 9

what is the best, efficient way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

明媚如初 2024-12-17 11:19:06

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$b ## [1] 2

hashmap

参见 https://cran.r-project.org/web/packages/hashmap/README.html

hashmap 确实提供存储任意类型对象的灵活性。

键和值仅限于“标量”对象(长度为一个字符、数字等)。这些值必须属于同一类型。

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

美丽的 print 实例:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

错误:

H[[2]] <- "Z"
## Error in x

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$b ## [1] 2

hashmap

参见 https://cran.r-project.org/web/packages/hashmap/README.html

hashmap 确实提供存储任意类型对象的灵活性。

键和值仅限于“标量”对象(长度为一个字符、数字等)。这些值必须属于同一类型。

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

美丽的 print 实例:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

错误:

[[<-`(i, value): Not compatible with requested type: [type=character; target=double]. H[[2]] <- c(1,3) ## Warning in x

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

values(h)
## 

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

1` ## [1] 42 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4` ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$a ## [1] 1 ## ##

我知道字典的三个 R 包:hashhashmapdict

2018 年 7 月更新:一个新的,容器

2018 年 9 月更新:一个新的,集合

hash

Key 必须是字符串。值可以是任何 R 对象。

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

获取键:

keys(h)
## [1] "1"   "4"   "foo"

获取值:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

print 实例:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

values 函数接受 sapply 的参数:

4`$b ## [1] 2

hashmap

参见 https://cran.r-project.org/web/packages/hashmap/README.html

hashmap 确实提供存储任意类型对象的灵活性。

键和值仅限于“标量”对象(长度为一个字符、数字等)。这些值必须属于同一类型。

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

美丽的 print 实例:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

错误:

[[<-`(i, value): length(keys) != length(values)!

dict

目前仅在 Github 上可用:https://github.com /mkuhn/dict

优点任意键和值,而且速度快。

library(dict)
d <- dict()
d[[1]] <- 42
d[[c(2, 3)]] <- "Hello!" # c(2,3) is the key
d[["foo"]] <- "bar"
d[[4]] <- list(a=1, b=2)
d[[1]]
## [1] 42
d[[c(2, 3)]]
## [1] "Hello!"
d[[4]]
## $a
## [1] 1
## 
## $b
## [1] 2

访问不存在的键会引发错误:

d[["not here"]]
## Error in d$get_or_stop(key): Key error: [1] "not here"

但是有一个很好的功能可以处理这个问题:

d$get("not here", "default value for missing key")
## [1] "default value for missing key"

获取键:

d$keys()
## [[1]]
## [1] 4
## 
## [[2]]
## [1] 1
## 
## [[3]]
## [1] 2 3
## 
## [[4]]
## [1] "foo"

获取值:

d$values()
## [[1]]
## [1] 42
## 
## [[2]]
## [1] "Hello!"
## 
## [[3]]
## [1] "bar"
## 
## [[4]]
## [[4]]$a
## [1] 1
## 
## [[4]]$b
## [1] 2

获取项目:

d$items()
## [[1]]
## [[1]]$key
## [1] 4
## 
## [[1]]$value
## [[1]]$value$a
## [1] 1
## 
## [[1]]$value$b
## [1] 2
## 
## 
## 
## [[2]]
## [[2]]$key
## [1] 1
## 
## [[2]]$value
## [1] 42
## 
## 
## [[3]]
## [[3]]$key
## [1] 2 3
## 
## [[3]]$value
## [1] "Hello!"
## 
## 
## [[4]]
## [[4]]$key
## [1] "foo"
## 
## [[4]]$value
## [1] "bar"

没有 print 实例。

该包还提供了函数 numvecdict 来处理字典,其中数字和字符串(包括每个字符串的向量)可以用作键,并且只能存储数字向量。

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$b ## [1] 2

hashmap

See https://cran.r-project.org/web/packages/hashmap/README.html.

hashmap does not offer the flexibility to store arbitrary types of objects.

Keys and values are restricted to "scalar" objects (length-one character, numeric, etc.). The values must be of the same type.

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

Beautiful print instance:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

Errors:

H[[2]] <- "Z"
## Error in x

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$b ## [1] 2

hashmap

See https://cran.r-project.org/web/packages/hashmap/README.html.

hashmap does not offer the flexibility to store arbitrary types of objects.

Keys and values are restricted to "scalar" objects (length-one character, numeric, etc.). The values must be of the same type.

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

Beautiful print instance:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

Errors:

[[<-`(i, value): Not compatible with requested type: [type=character; target=double]. H[[2]] <- c(1,3) ## Warning in x

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

values(h, USE.NAMES=FALSE)
## [[1]]
## [1] 42
## 
## [[2]]
## [[2]]$a
## [1] 1
## 
## [[2]]$b
## [1] 2
## 
## 
## [[3]]
## [1] "bar"
values(h, keys="4")
##   4
## a 1
## b 2
values(h, keys="4", simplify=FALSE)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

values(h)
## 

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

1` ## [1] 42 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4` ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$a ## [1] 1 ## ##

I know three R packages for dictionaries: hash, hashmap, and dict.

Update July 2018: a new one, container.

Update September 2018: a new one, collections

hash

Keys must be character strings. A value can be any R object.

library(hash)
## hash-2.2.6 provided by Decision Patterns
h <- hash() 
# set values
h[["1"]] <- 42
h[["foo"]] <- "bar"
h[["4"]] <- list(a=1, b=2)
# get values
h[["1"]]
## [1] 42
h[["4"]]
## $a
## [1] 1
## 
## $b
## [1] 2
h[c("1", "foo")]
## <hash> containing 2 key-value pair(s).
##   1 : 42
##   foo : bar
h[["key not here"]]
## NULL

To get keys:

keys(h)
## [1] "1"   "4"   "foo"

To get values:

4`$b ## [1] 2 ## ## ## $foo ## [1] "bar"

The print instance:

h
## <hash> containing 3 key-value pair(s).
##   1 : 42
##   4 : 1 2
##   foo : bar

The values function accepts the arguments of sapply:

4`$b ## [1] 2

hashmap

See https://cran.r-project.org/web/packages/hashmap/README.html.

hashmap does not offer the flexibility to store arbitrary types of objects.

Keys and values are restricted to "scalar" objects (length-one character, numeric, etc.). The values must be of the same type.

library(hashmap)
H <- hashmap(c("a", "b"), rnorm(2))
H[["a"]]
## [1] 0.1549271
H[[c("a","b")]]
## [1]  0.1549271 -0.1222048
H[[1]] <- 9

Beautiful print instance:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]

Errors:

[[<-`(i, value): length(keys) != length(values)!

dict

Currently available only on Github: https://github.com/mkuhn/dict

Strengths: arbitrary keys and values, and fast.

library(dict)
d <- dict()
d[[1]] <- 42
d[[c(2, 3)]] <- "Hello!" # c(2,3) is the key
d[["foo"]] <- "bar"
d[[4]] <- list(a=1, b=2)
d[[1]]
## [1] 42
d[[c(2, 3)]]
## [1] "Hello!"
d[[4]]
## $a
## [1] 1
## 
## $b
## [1] 2

Accessing to a non-existing key throws an error:

d[["not here"]]
## Error in d$get_or_stop(key): Key error: [1] "not here"

But there is a nice feature to deal with that:

d$get("not here", "default value for missing key")
## [1] "default value for missing key"

Get keys:

d$keys()
## [[1]]
## [1] 4
## 
## [[2]]
## [1] 1
## 
## [[3]]
## [1] 2 3
## 
## [[4]]
## [1] "foo"

Get values:

d$values()
## [[1]]
## [1] 42
## 
## [[2]]
## [1] "Hello!"
## 
## [[3]]
## [1] "bar"
## 
## [[4]]
## [[4]]$a
## [1] 1
## 
## [[4]]$b
## [1] 2

Get items:

d$items()
## [[1]]
## [[1]]$key
## [1] 4
## 
## [[1]]$value
## [[1]]$value$a
## [1] 1
## 
## [[1]]$value$b
## [1] 2
## 
## 
## 
## [[2]]
## [[2]]$key
## [1] 1
## 
## [[2]]$value
## [1] 42
## 
## 
## [[3]]
## [[3]]$key
## [1] 2 3
## 
## [[3]]$value
## [1] "Hello!"
## 
## 
## [[4]]
## [[4]]$key
## [1] "foo"
## 
## [[4]]$value
## [1] "bar"

No print instance.

The package also provides the function numvecdict to deal with a dictionary in which numbers and strings (including vectors of each) can be used as keys, and that can only store vectors of numbers.

浪推晚风 2024-12-17 11:19:06

您只需使用键值对创建一个向量即可。

animal_sounds <- c(
  'cat' = 'meow',
  'dog' = 'woof',
  'cow' = 'moo'
)
print(animal_sounds['cat'])
# 'meow'

更新:要回答问题的第二部分,您可以创建一个数据框并计算如下值:

val1 <- c(5,2,5,2) # Create val1 column
val2 <- c(5,5,2,2) # Create val2 column
df <- data.frame(val1, val2) # create dataframe variable
df['x1^2+x2'] <- val1^2 + val2 # create expression column

输出:

  val1 val2 x1^2+x2
1    5    5      30
2    2    5       9
3    5    2      27
4    2    2       6

You simply create a vector with your key value pairs.

animal_sounds <- c(
  'cat' = 'meow',
  'dog' = 'woof',
  'cow' = 'moo'
)
print(animal_sounds['cat'])
# 'meow'

Update: To answer the 2nd portion of the question, you can create a dataframe and compute the values like this:

val1 <- c(5,2,5,2) # Create val1 column
val2 <- c(5,5,2,2) # Create val2 column
df <- data.frame(val1, val2) # create dataframe variable
df['x1^2+x2'] <- val1^2 + val2 # create expression column

Output:

  val1 val2 x1^2+x2
1    5    5      30
2    2    5       9
3    5    2      27
4    2    2       6
岁月流歌 2024-12-17 11:19:06

您可以仅使用 data.framerow.names 来执行此操作:

x=data.frame(row.names=c("Hi","Why","water") , val=c(1,5,4))
x["Why",]
[1] 5

You can use just data.frame and row.names to do this:

x=data.frame(row.names=c("Hi","Why","water") , val=c(1,5,4))
x["Why",]
[1] 5
那小子欠揍 2024-12-17 11:19:06

在向量、矩阵、列表等在 R 中充当“字典”的情况下,您可以执行如下操作:

> (x <- structure(c(5,2),names=c("a","b"))) ## "dictionary"
a b 
5 2 
> (result <- outer(x,x,function(x1,x2) x1^2+x2))
   a  b
a 30 27
b  9  6
> result["b","a"]
[1] 9

如果您想要一个如示例中所示的表格,只需重新调整数组的形状...

> library(reshape)
> (dfr <- melt(result,varnames=c("x1","x2")))
  x1 x2 value
1  a  a    30
2  b  a     9
3  a  b    27
4  b  b     6
> transform(dfr,val1=x[x1],val2=x[x2])
  x1 x2 value val1 val2
1  a  a    30    5    5
2  b  a     9    2    5
3  a  b    27    5    2
4  b  b     6    2    2

In that vectors, matrices, lists, etc. behave as "dictionaries" in R, you can do something like the following:

> (x <- structure(c(5,2),names=c("a","b"))) ## "dictionary"
a b 
5 2 
> (result <- outer(x,x,function(x1,x2) x1^2+x2))
   a  b
a 30 27
b  9  6
> result["b","a"]
[1] 9

If you wanted a table as you've shown in your example, just reshape your array...

> library(reshape)
> (dfr <- melt(result,varnames=c("x1","x2")))
  x1 x2 value
1  a  a    30
2  b  a     9
3  a  b    27
4  b  b     6
> transform(dfr,val1=x[x1],val2=x[x2])
  x1 x2 value val1 val2
1  a  a    30    5    5
2  b  a     9    2    5
3  a  b    27    5    2
4  b  b     6    2    2
×纯※雪 2024-12-17 11:19:06

请参阅我的答案最近的一个问题。本质上,您使用环境来实现此类功能。

对于高维情况,如果您想要使用简单的语法来检索结果(您可以命名行和列),则最好使用数组(二维)。作为替代方案,您可以将两个键与它们中不存在的分隔符粘贴在一起,然后将其用作唯一标识符。

具体来说,是这样的:

tmp<-data.frame(x=c("a", "b"), val=c(5,2))
tmp2<-outer(seq(nrow(tmp)), seq(nrow(tmp)), function(lhs, rhs){tmp$val[lhs] + tmp$val[rhs]})
dimnames(tmp2)<-list(tmp$x, tmp$x)
tmp2
tmp2["a", "b"]

See my answer to a very recent question. In essence, you use environments for this type of functionality.

For the higher dimensional case, you may be better off using an array (twodimensional) if you want the easy syntax for retrieving the result (you can name the rows and columns). As an alternative,you can paste together the two keys with a separator that doesn't occur in them, and then use that as a unique identifier.

To be specific, something like this:

tmp<-data.frame(x=c("a", "b"), val=c(5,2))
tmp2<-outer(seq(nrow(tmp)), seq(nrow(tmp)), function(lhs, rhs){tmp$val[lhs] + tmp$val[rhs]})
dimnames(tmp2)<-list(tmp$x, tmp$x)
tmp2
tmp2["a", "b"]
自由如风 2024-12-17 11:19:06

使用 tidyverse

使用更新的 tidyverse 方法添加答案。

可能有更干净的方法来处理交叉(创建所有组合)和取消嵌套,但这是一种快速而肮脏的方法。

library(tidyverse)

my_tbl <- tibble(x = c("A", "B"), val=c(5,2)) %>% 
  crossing(x1 = ., x2 = .) %>%  # Create all combinations
  unnest_wider(everything(), names_sep="_") %>% # Unpack into distinct columns
  mutate(result = x1_val^2 + x2_val)  # Calculate result

# Access result by accessing the row in the data frame
my_tbl %>% 
  filter(x1_x == "A", x2_x == "B") %>% 
  pull(result)
#> [1] 27

# Convert tibble to a named vector that could be accessed more easily.
# However, this is limited to string names.
my_named_vector <- my_tbl %>% 
  transmute(name = str_c(x1_x, "_", x2_x), value=result) %>% 
  deframe()

my_named_vector[["A_B"]]
#> [1] 27

reprex 包 (v2.0.1) 于 2022 年 4 月 6 日创建

<代码>tibble 版本 3.1.6
dplyr 版本 1.0.8
tidyr 版本 1.2.0
stringr 版本 1.4.0

Using tidyverse

Adding an answer using more recent tidyverse approaches.

There are probably cleaner ways of handling the crossing (which creates all combinations) and unnesting, but this is a quick and dirty approach.

library(tidyverse)

my_tbl <- tibble(x = c("A", "B"), val=c(5,2)) %>% 
  crossing(x1 = ., x2 = .) %>%  # Create all combinations
  unnest_wider(everything(), names_sep="_") %>% # Unpack into distinct columns
  mutate(result = x1_val^2 + x2_val)  # Calculate result

# Access result by accessing the row in the data frame
my_tbl %>% 
  filter(x1_x == "A", x2_x == "B") %>% 
  pull(result)
#> [1] 27

# Convert tibble to a named vector that could be accessed more easily.
# However, this is limited to string names.
my_named_vector <- my_tbl %>% 
  transmute(name = str_c(x1_x, "_", x2_x), value=result) %>% 
  deframe()

my_named_vector[["A_B"]]
#> [1] 27

Created on 2022-04-06 by the reprex package (v2.0.1)

tibble version 3.1.6
dplyr version 1.0.8
tidyr version 1.2.0
stringr version 1.4.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文