我有一個微不足道的問題:我找不到R中的字典數據結構,所以我用列表來代替(如「word」 - > number) 因此,現在我有問題獲取密鑰列表。 有人知道嗎?在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"
>
你可能想看看在CRAN的hash
package。
如果你的「數字」值都是相同的模式,你甚至不需要列表。如果我把德克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
如果元素數量不固定,則列表也很好。 – skan 2017-07-24 18:13:55
@skan所以是矢量... – Calimo 2017-07-24 18:15:36
你怎麼能得到這個字典式的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
要延長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」]]) ]
我可以添加包含空格的鍵嗎?像'奇怪的鍵'? – user1700890 2017-03-23 16:45:08
也像這樣的東西不行'c(foo,tic2 = NULL)'。任何工作? – user1700890 2017-03-23 17:14:34
我只是評論你可以得到很多的里程數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"
等
我不認爲'as.numeric()'是必須的。該表已經是數字。你可以用'names(t [order(t)]')得到相同的結果' – 2015-05-21 15:48:22
德克的回答更短的變化:
# 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"
原因首先使用字典中的表現。雖然您可以使用命名向量和列表來完成任務,但問題在於它們變得非常慢並且內存越來越大,需要更多數據。
然而,許多人不知道的是的是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
它是否適用於多值關係? 例如tic = 1和tic = 17 – skan 2017-07-24 18:16:36
包哈希現已: 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"
如何添加多個值?我試着重複這個鍵,但它只存儲最後一個值。我也嘗試分配列表,但它不起作用 – skan 2017-07-24 18:27:34
- 1. 使用Python字典/列表
- 2. 字典使用列表
- 3. 在R中使用字典/列表:如何從鍵值對中獲取值?
- 4. 在R中使用列表
- 5. 返回使用R「字典」
- 6. python字典 - 列表字典
- 7. 從字典列表字典
- 8. SQLAlchemy - 使用字典列表更新表
- 9. Python子字典/字典中的列表
- 10. 在字典查找中使用Python列表或字符串?
- 11. 列表字典
- 12. 如何使用列表和另一個字典在python中創建字典
- 13. 從Python中的字典的現有列表字典的列表
- 14. 將字典列表寫入csv,其中字典值是列表
- 15. 列表字典列表
- 16. 在字典列表中搜索列表中的字符串
- 17. 使用字典()內建的元組列表生成字典
- 18. 使用字典鍵過濾列表
- 19. 使用Javascript的字典訪問列表
- 20. 使用字典與列表操作
- 21. 列表到字典轉換使用Linq
- 22. Python - 使用列表創建字典
- 23. 列表使用LINQ單個字典
- 24. python字典使用defaultdict(列表)
- 25. 列表理解使用字典條目
- 26. 轉換列表以字典使用python
- 27. 使用列表條目作爲字典
- 28. 在Python中如何使用字典中的列表?
- 29. 如何訪問R中哈希env列表中的列表,如Python字典
- 30. 閱讀列表中的字典詞典
+1回答問題時根本不關心OP的無效的方法字。 – Marek 2010-05-19 12:47:44