2010-05-18 42 views
59

我有一個微不足道的問題:我找不到R中的字典數據結構,所以我用列表來代替(如「word」 - > number) 因此,現在我有問題獲取密鑰列表。 有人知道嗎?在R中使用字典/列表

回答

79

是的,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" 
> 
+6

+1回答問題時根本不關心OP的無效的方法字。 – Marek 2010-05-19 12:47:44

4

你可能想看看在CRAN的hashpackage

39

如果你的「數字」值都是相同的模式,你甚至不需要列表。如果我把德克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 
+0

如果元素數量不固定,則列表也很好。 – skan 2017-07-24 18:13:55

+0

@skan所以是矢量... – Calimo 2017-07-24 18:15:36

+0

你怎麼能得到這個字典式的R結構foo列表'c(12,22,33)'? 'unlist(lapply(FUN = function(a){foo [[a]]},X = 1:length(foo)))'非常不方便。任何準備好的功能?移動問題[這裏](https://stackoverflow.com/questions/45460273/working-with-dictionaries-lists-in-r-how-to-get-the-values-from-the-key-value-p ) – hhh 2017-08-02 12:02:34

11

要延長Calimo一點點的回答我提出幾個在R中創建此準詞典時可能會發現有用的東西:

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

>as.numeric(foo) 
[1] 12 22 33 

b)檢查字典中是否包含KEY:

>'tic' %in% names(foo) 
[1] TRUE 

c)中如何添加新鍵,值piar到詞典:

C(FOO,tic2 = 44)

結果:

tic  tac  toe  tic2 
12  22  33  44 

d)如何填寫REAL DICTIONARY的要求 - 那個鍵不能重複(UNIQU KEYS)?你需要結合b)和c)來構建函數來驗證是否存在這樣的密鑰,並且做你想做的事情:例如,不允許插入,如果新的不同於舊的,或者以某種方式重建密鑰,則更新值(例如,增加了一些數字它,因此它是唯一的)

E)如何從詞典中刪除對BY KEY:

FOO < -foo [其中(富= FOO [[ 「TAC」]]) ]

+0

我可以添加包含空格的鍵嗎?像'奇怪的鍵'? – user1700890 2017-03-23 16:45:08

+0

也像這樣的東西不行'c(foo,tic2 = NULL)'。任何工作? – user1700890 2017-03-23 17:14:34

4

我只是評論你可以得到很多的里程數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" 

+0

我不認爲'as.numeric()'是必須的。該表已經是數字。你可以用'names(t [order(t)]')得到相同的結果' – 2015-05-21 15:48:22

4

德克的回答更短的變化:

# 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" 
8

原因首先使用字典中的表現。雖然您可以使用命名向量和列表來完成任務,但問題在於它們變得非常慢並且內存越來越大,需要更多數據。

然而,許多人不知道的是的是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 
+0

它是否適用於多值關係? 例如tic = 1和tic = 17 – skan 2017-07-24 18:16:36

4

哈希現已: 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" 
+0

如何添加多個值?我試着重複這個鍵,但它只存儲最後一個值。我也嘗試分配列表,但它不起作用 – skan 2017-07-24 18:27:34