list 如何将变量键/值对添加到列表对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1105659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:22:03  来源:igfitidea点击:

How to add variable key/value pair to list object?

listr

提问by Frank

I have two variables, keyand value, and I want to add them as a key/value pair to a list:

我有两个变量keyand value,我想将它们作为键/值对添加到列表中:

key = "width"
value = 32

mylist = list()
mylist$key = value

The result is this:

结果是这样的:

mylist
# $key
# [1] 32

But I would like this instead:

但我想要这个:

mylist
# $width
# [1] 32

How can I do this?

我怎样才能做到这一点?

回答by Sharpie

R lists can be thought of as hashes- vectors of objects that can be accessed by name. Using this approach you can add a new entry to the list like so:

R 列表可以被认为是可以通过名称访问的对象的哈希向量。使用这种方法,您可以向列表中添加一个新条目,如下所示:

key <- "width"
value <- 32

mylist <- list()
mylist[[ key ]] <- value

Here we use the string stored in the variable key to access a position in the list much like using the value stored in a loop variable i to access a vector through:

这里我们使用存储在变量 key 中的字符串来访问列表中的位置,就像使用存储在循环变量 i 中的值访问向量一样:

vector[ i ]

The result is:

结果是:

myList
$width
[1] 32

回答by Assad Ebrahim

The setNames()built-in function makes it easy to create a hash from given key and value lists. (Thanks to Nick K for the better suggestion.)

setNames()内置功能可以很容易地创建一个从给定的密钥和值列表的散列。 (感谢 Nick K 提供更好的建议。)

Usage: hh <- setNames(as.list(values), keys)

用法: hh <- setNames(as.list(values), keys)

Example:

例子:

players <- c("bob", "tom", "tim", "tony", "tiny", "hubert", "herbert")
rankings <- c(0.2027, 0.2187, 0.0378, 0.3334, 0.0161, 0.0555, 0.1357)
league <- setNames(as.list(rankings), players)

Then accessing the values through the keys is easy:

然后通过键访问值很容易:

league$bob
 [1] 0.2027
league$hubert
 [1] 0.0555
league$bob
 [1] 0.2027
league$hubert
 [1] 0.0555

回答by mpiktas

List elements in R can be named. So in your case just do

R 中的列表元素可以命名。所以在你的情况下就做

 > mylist = list()
 > mylist$width = value

When R encounters this code

当 R 遇到这段代码时

> l$somename=something

where l is a list. It appends to a list an element something, and names it with name somename. It is then can be accessed by using

其中 l 是一个列表。它将一个元素添加到一个列表中,并用名称 somename 命名它。然后可以通过使用访问它

> l[["somename"]]

or

或者

> l$somename

The name can be changed with command names:

可以使用命令名称更改名称:

> names(l)[names(l)=="somename"] <- "othername"

Or if you now the position of the element in the list by:

或者,如果您现在通过以下方式确定元素在列表中的位置:

> names(l)[1] <- "someothername"

回答by AutomationNerd

We can use R's list data structure to store data in the form of key-value pair.

我们可以使用 R 的列表数据结构以键值对的形式存储数据。

Syntax:

句法:

ObjectName<-list("key"= value)

ObjectName<-list("key"= value)

Example:

例子:

mylist<-list("width"=32)

mylist<-list("width"=32)

also, refer example: "https://github.com/WinVector/zmPDSwR/blob/master/Statlog/GCDSteps.R"

另外,请参考示例:“https://github.com/WinVector/zmPDSwR/blob/master/Statlog/GCDSteps.R”