我有一个小问题:在R中没有找到字典数据结构,所以我使用了列表(类似于"单词"->数字
)。那么,我如何获取键的列表。
是的,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"
>
如果你的“number”值都是相同类型的,那么甚至不需要使用列表。如果我拿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"
只有当你的值为混合模式(例如字符和数字)或向量时,才需要使用列表。
对于列表和向量,可以通过名称子集化单个元素:
> foo["tac"]
tac
22
或者针对列表:
> foo[["tac"]]
[1] 22
c(12,22,33)
?unlist(lapply(FUN=function(a){foo[[a]]},X = 1:length(foo)))
非常不方便。有没有现成的函数可以实现这个功能?问题已经转移到这里。 - hhh在Calimo的回答基础上,我再补充一些关于在R中创建这种准字典的有用信息:
a) 如何返回字典中所有的值:
>as.numeric(foo)
[1] 12 22 33
b) 检查字典是否包含关键字:
>'tic' %in% names(foo)
[1] TRUE
c) 如何向字典中添加新的键值对:
c(foo, tic2=44)
结果:
tic tac toe tic2
12 22 33 44
d) 如何满足真实字典的要求 - 键不能重复(唯一键)?您需要结合 b) 和 c) 并构建一个功能,该功能验证是否存在这样的键,并执行所需操作:例如不允许插入,如果新值与旧值不同,则更新值,或以某种方式重建键(例如添加一些数字使其唯一)
e) 如何按键从字典中删除键值对:
foo <- foo[which(foo!=foo[["tac"]])]
c(foo, tic2=NULL)
。有什么解决方法吗? - user1700890使用字典的原因在于性能。虽然使用命名向量和列表也可以完成任务,但问题在于随着数据增加,它们变得非常缓慢且占用大量内存。
然而,许多人不知道的是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
编辑:基于这个答案,我写了一篇博客文章,提供了更多背景信息:http://blog.ephorie.de/hash-me-if-you-can
现在可以使用hash包了: 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"
Dirk的回答简化版:
# 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"
我只想说,当你尝试“伪造”一个字典时,table
可以提供很多帮助。
> x <- c("a","a","b","b","b","c")
> (t <- table(x))
x
a b c
2 3 1
> names(t)
[1] "a" "b" "c"
> o <- order(as.numeric(t))
> names(t[o])
[1] "c" "a" "b"
etc.
as.numeric()
不是必要的。表格已经是数字类型了。你可以使用 names(t[order(t)])
得到相同的结果。 - Rich Scriven