如何向列表对象添加变量键/值对?

时间:2021-03-20 19:27:36

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

我有两个变量,key和value,我想把它们作为key/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?

我该怎么做呢?

4 个解决方案

#1


61  

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:

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

vector[ i ]

The result is:

其结果是:

myList
$width
[1] 32

#2


13  

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"

#3


7  

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(值)、key)

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

#4


0  

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 <附些(“关键”=价值)< p>

Example:

例子:

mylist<-list("width"=32)

mylist <附些(“宽度”= 32)< p>

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

同时,引用的例子:“https://github.com/WinVector/zmPDSwR/blob/master/Statlog/GCDSteps.R”

#1


61  

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:

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

vector[ i ]

The result is:

其结果是:

myList
$width
[1] 32

#2


13  

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"

#3


7  

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(值)、key)

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

#4


0  

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 <附些(“关键”=价值)< p>

Example:

例子:

mylist<-list("width"=32)

mylist <附些(“宽度”= 32)< p>

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

同时,引用的例子:“https://github.com/WinVector/zmPDSwR/blob/master/Statlog/GCDSteps.R”