R中是否有字典功能

era*_*ran 10 dictionary r

有没有办法在R中创建一个"字典",以便它有成对?有效的东西:

x=dictionary(c("Hi","Why","water") , c(1,5,4))
x["Why"]=5
Run Code Online (Sandbox Code Playgroud)

我问这个是因为我实际上在寻找两个分类变量函数.

所以,如果x =字典(c("a","b"),c(5,2))

     x  val
1    a  5 
2    b  2 
Run Code Online (Sandbox Code Playgroud)

我想在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
Run Code Online (Sandbox Code Playgroud)

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

什么是最好,最有效的方法呢?

tou*_*mes 33

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

animal_sounds <- c(
  'cat' = 'meow',
  'dog' = 'woof',
  'cow' = 'moo'
)
Run Code Online (Sandbox Code Playgroud)
print(animal_sounds['cat'])
# 'meow'
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

输出:

  val1 val2 x1^2+x2
1    5    5      30
2    2    5       9
3    5    2      27
4    2    2       6
Run Code Online (Sandbox Code Playgroud)


Sté*_*ent 10

我知道字典三个R包:hash,hashmap,和dict.

2018年7月更新:新的,container.

2018年9月更新:新的收藏品

哈希

键必须是字符串.值可以是任何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
Run Code Online (Sandbox Code Playgroud)

要获得钥匙:

keys(h)
## [1] "1"   "4"   "foo"
Run Code Online (Sandbox Code Playgroud)

要获得价值:

values(h)
## $`1`
## [1] 42
## 
## $`4`
## $`4`$a
## [1] 1
## 
## $`4`$b
## [1] 2
## 
## 
## $foo
## [1] "bar"
Run Code Online (Sandbox Code Playgroud)

print实例:

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

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

美丽的print实例:

H
## ## (character) => (numeric)  
## ##         [1] => [+9.000000]
## ##         [b] => [-0.122205]
## ##         [a] => [+0.154927]
Run Code Online (Sandbox Code Playgroud)

错误:

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)!
Run Code Online (Sandbox Code Playgroud)

字典

目前仅在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
Run Code Online (Sandbox Code Playgroud)

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

d[["not here"]]
## Error in d$get_or_stop(key): Key error: [1] "not here"
Run Code Online (Sandbox Code Playgroud)

但有一个很好的功能来处理:

d$get("not here", "default value for missing key")
## [1] "default value for missing key"
Run Code Online (Sandbox Code Playgroud)

获取密钥:

d$keys()
## [[1]]
## [1] 4
## 
## [[2]]
## [1] 1
## 
## [[3]]
## [1] 2 3
## 
## [[4]]
## [1] "foo"
Run Code Online (Sandbox Code Playgroud)

获取价值:

d$values()
## [[1]]
## [1] 42
## 
## [[2]]
## [1] "Hello!"
## 
## [[3]]
## [1] "bar"
## 
## [[4]]
## [[4]]$a
## [1] 1
## 
## [[4]]$b
## [1] 2
Run Code Online (Sandbox Code Playgroud)

获取物品:

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"
Run Code Online (Sandbox Code Playgroud)

没有print实例.

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

  • [hatmatrix 的列表、矩阵等答案](/sf/answers/547402271/) 有什么问题吗?如果列表具有与这些解决方案相同的性能,那么它们似乎将是最普遍认可的方法。 (2认同)

xm1*_*xm1 7

你可以使用 justdata.framerow.names来做到这一点:

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


hat*_*rix 6

由于向量、矩阵、列表等在 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 赋值表达式周围的括号只是为了打印结果。 (2认同)

Nic*_*bbe 1

请参阅我对最近一个问题的回答。本质上,您使用环境来实现此类功能。

array对于更高维的情况,如果您想要使用简单的语法来检索结果(您可以命名行和列),则最好使用(二维)。作为替代方案,您可以将paste两个键与它们中不存在的分隔符组合在一起,然后将其用作唯一标识符。

具体来说,是这样的:

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"]
Run Code Online (Sandbox Code Playgroud)