R中有字典功能吗 [英] Is there a dictionary functionality in R

查看:341
本文介绍了R中有字典功能吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在R中创建字典",使其具有成对的? 可以起到以下作用的

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.

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

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

     x  val
1    a  5 
2    b  2 

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

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

然后我希望能够使用x1和x2检索结果.效果: get_result ["b","a"] = 9

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?

推荐答案

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

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

2018年7月更新: :一个新的 2018年9月更新: :一个新的键必须是字符串.值可以是任何R对象.

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

要获取密钥:

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

要获取值:

values(h)
## $`1`
## [1] 42
## 
## $`4`
## $`4`$a
## [1] 1
## 
## $`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)
## $`4`
## $`4`$a
## [1] 1
## 
## $`4`$b
## [1] 2

哈希图

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

hashmap不提供 not 来提供存储任意类型对象的灵活性.

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

键和值仅限于标量"对象(长度为1的字符,数字等).这些值必须是相同的类型.

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

美丽的print实例:

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

错误:

H[[2]] <- "Z"
## Error in x$`[[<-`(i, value): Not compatible with requested type: [type=character; target=double].
H[[2]] <- c(1,3)
## Warning in x$`[[<-`(i, value): length(keys) != length(values)!

dict

当前仅在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"

获取密钥:

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来处理字典,在字典中数字和字符串(包括每个向量)可以用作键,并且只能存储数字向量.

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.

这篇关于R中有字典功能吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆