使用R中的词典/列表

Ivr*_*vri 82 lookup r list hashmap

我有一个小问题:我在R中找不到字典数据结构,所以我使用了list而不是(比如"word" - > number)所以,现在我有问题如何获取键列表.有人知道吗?

Dir*_*tel 107

是的,list类型是一个很好的近似值.您可以names()在列表中使用来设置和检索"键":

> foo <- vector(mode="list", length=3)
> names(foo) <- c("tic", "tac", "toe")
> foo[[1]] <- 12; foo[[2]] <- 22; foo[[3]] <- 33
> foo
$tic
[1] 12

$tac
[1] 22

$toe
[1] 33

> names(foo)
[1] "tic" "tac" "toe"
> 
Run Code Online (Sandbox Code Playgroud)

  • +1回答问题,一言不发OP的无效方法. (11认同)
  • 是的,R中使用了`environment`类型,但是它不太常见/鲜为人知。 (3认同)
  • 根据列表作为字典代理的预期用途,请谨记,对列表的“关键”查找是O(n)而不是O(1),这是您期望的字典(散列键)。 (2认同)

Cal*_*imo 50

如果您的"数字"值都是相同模式,则甚至不需要列表.如果我拿Dirk Eddelbuettel的例子:

> foo <- c(12, 22, 33)
> names(foo) <- c("tic", "tac", "toe")
> foo
tic tac toe
 12  22  33
> names(foo)
[1] "tic" "tac" "toe"
Run Code Online (Sandbox Code Playgroud)

仅当您的值是混合模式(例如字符和数字)或向量时,才需要列表.

对于列表和向量,可以按名称对单个元素进行子集化:

> foo["tac"]
tac 
 22 
Run Code Online (Sandbox Code Playgroud)

或者列表:

> foo[["tac"]]
[1] 22
Run Code Online (Sandbox Code Playgroud)


and*_*abs 16

为了扩展Calimo的一点回答,我在R中创建这个准词典时,可能会发现更多有用的东西:

a)如何返回字典的所有VALUES:

>as.numeric(foo)
[1] 12 22 33
Run Code Online (Sandbox Code Playgroud)

b)检查字典CONTAINS KEY:

>'tic' %in% names(foo)
[1] TRUE
Run Code Online (Sandbox Code Playgroud)

c)如何添加新密钥,值piar到字典:

C(FOO,tic2 = 44)

结果:

tic       tac       toe     tic2
12        22        33        44 
Run Code Online (Sandbox Code Playgroud)

d)如何满足REAL DICTIONARY的要求 - 那些键不能重复(UNIQU键)?你需要结合b)和c)和构建函数来验证是否有这样的键,并做你想做的事情:例如,不允许插入,更新值,如果新旧的不同,或重建某种方式键(例如添加一些数字,因此它是独一无二的)

e)如何从字典中删除对BY KEY:

FOO <-foo [其中(FOO!= FOO [[ "TAC"]])]


von*_*njd 14

首先使用字典的原因是性能.虽然你可以使用命名向量和列表来完成任务是正确的,但问题是它们变得非常慢并且内存很多,需要更多数据.

然而,许多人不知道的是,R 确实有一个内置的字典数据结构:具有选项的环境hash = TRUE

有关如何使其工作的信息,请参阅以下示例:

# vectorize assign, get and exists for convenience
assign_hash <- Vectorize(assign, vectorize.args = c("x", "value"))
get_hash <- Vectorize(get, vectorize.args = "x")
exists_hash <- Vectorize(exists, vectorize.args = "x")

# keys and values
key<- c("tic", "tac", "toe")
value <- c(1, 22, 333)

# initialize hash
hash = new.env(hash = TRUE, parent = emptyenv(), size = 100L)
# assign values to keys
assign_hash(key, value, hash)
## tic tac toe 
##   1  22 333
# get values for keys
get_hash(c("toe", "tic"), hash)
## toe tic 
## 333   1
# alternatively:
mget(c("toe", "tic"), hash)
## $toe
## [1] 333
## 
## $tic
## [1] 1
# show all keys
ls(hash)
## [1] "tac" "tic" "toe"
# show all keys with values
get_hash(ls(hash), hash)
## tac tic toe 
##  22   1 333
# remove key-value pairs
rm(list = c("toe", "tic"), envir = hash)
get_hash(ls(hash), hash)
## tac 
##  22
# check if keys are in hash
exists_hash(c("tac", "nothere"), hash)
##     tac nothere 
##    TRUE   FALSE
# for single keys this is also possible:
# show value for single key
hash[["tac"]]
## [1] 22
# create new key-value pair
hash[["test"]] <- 1234
get_hash(ls(hash), hash)
##  tac test 
##   22 1234
# update single value
hash[["test"]] <- 54321
get_hash(ls(hash), hash)
##   tac  test 
##    22 54321
Run Code Online (Sandbox Code Playgroud)

编辑:在这个答案的基础上,我写了一篇博文,内容更多:http://blog.ephorie.de/hash-me-if-you-can


小智 8

哈希现在可用:https: //cran.r-project.org/web/packages/hash/hash.pdf

例子

h <- hash( keys=letters, values=1:26 )
h <- hash( letters, 1:26 )
h$a
# [1] 1
h$foo <- "bar"
h[ "foo" ]
# <hash> containing 1 key-value pair(s).
#   foo : bar
h[[ "foo" ]]
# [1] "bar"
Run Code Online (Sandbox Code Playgroud)


Jyo*_*rya 5

您可能想查看CRAN 上的hash .


Net*_*tle 5

德克答案的变化较小:

# Create a Color Palette Dictionary 
> color <- c('navy.blue', 'gold', 'dark.gray')
> hex <- c('#336A91', '#F3C117', '#7F7F7F')

> # Create List
> color_palette <- as.list(hex)
> # Name List Items
> names(color_palette) <- color
> 
> color_palette
$navy.blue
[1] "#336A91"

$gold
[1] "#F3C117"

$dark.gray
[1] "#7F7F7F"
Run Code Online (Sandbox Code Playgroud)