list 如何在R中创建列表向量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2624791/
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:28:48  来源:igfitidea点击:

How to create a vector of lists in R?

rlistvector

提问by Martin

I have a list (tmpList), which looks like this:

我有一个列表 (tmpList),如下所示:

$op
[1] "empty"

$termset
$termset$field
[1] "entry"

$termset[[2]]
$termset[[2]]$explode
[1] "Y"

This is a list with a list inside. If I add this list to a vector

这是一个列表,里面有一个列表。如果我将此列表添加到向量

theOneVector = c(theOneVector, tmpList)

Now the resulting vector is of the length 2, because the first entry ("op") of the list is separated from the tmpList. Is it possible to append the complete tmpList into this vector?
I already tried it with

现在结果向量的长度为 2,因为列表的第一个条目(“op”)与 tmpList 分开。是否可以将完整的 tmpList 附加到这个向量中?
我已经试过了

theOneVector = c(theOneVector, list(tmpList))

which gives a vector with the length of 1, but it is very cumbersome to access the elements of the list with this extra list around the list. (Too much list in one sentence I think.)

它给出了一个长度为 1 的向量,但是用这个围绕列表的额外列表来访问列表的元素是非常麻烦的。(我认为一句话列出太多了。)

Any help would be appreciated,
Martin

任何帮助将不胜感激,
马丁

回答by Dirk Eddelbuettel

You can stick a vector (a restricted structure where all components have to be of the same type) into a list (unrestricted).

您可以将向量(所有组件必须属于同一类型的受限结构)粘贴到列表中(不受限制)。

But you cannot do the reverse. Use lists of lists of lists ... and then use lapply et al to extract.

但你不能反过来。使用列表的列表列表......然后使用 lapply 等人来提取。

回答by doug

the expression 'c(theOneVector, list(tmpList))' actually didn't return a vector of length 1, it returned a list (by coersion) because the items in a vector must all be of the same mode (data type).

表达式 'c(theOneVector, list(tmpList))' 实际上没有返回长度为 1 的向量,它返回了一个列表(通过强制转换),因为向量中的项必须都具有相同的模式(数据类型)。

Here's what you can do to create a container in R that will hold items of different mode and whose items are easy to access:

以下是在 R 中创建一个容器的方法,该容器将保存不同模式的项目并且其项目易于访问:

# create the container (an R 'list')
vx = vector(mode="list")

# create some items having different modes to put in it
item1 = 1:5
item2 = "another item"
item3 = 34
item4 = list(a = c(1:5), b = c(10:15))

# now fill the container 
vx$item1 = item1
vx$item2 = item2
vx$item3 = item3
vx$item4 = item4

# access the items in the container by name:
vx$item1
# returns: [1] 4 5 6
vx$item2
# returns: [1] "another item"

回答by Raphvanns

I think a list()of list()is what you want.

我认为 a list()oflist()是你想要的。

Here is a simple example with lapply()which shows how to use them. Note that lapply()will apply the function provided to each element of the list given in argument and return a list containing the results of the individual executions.

这是一个简单的例子lapply(),展示了如何使用它们。请注意,这lapply()会将提供的函数应用于参数中给出的列表的每个元素,并返回一个包含各个执行结果的列表。

> l1 = list(a = 10, b = 11)
> l2 = list(a = 20, b = 22)

> test_function <- function(l){
   return(paste("a =", l$a, "b = ", l$b, "\n"))
  }


# Do something to each element of the list 
# (i.e.: apply a function test_function() using lapply()). 
# This will return a list over which you can iterate.
# Each individual list l1 and l2 is "wrapped" into a single list: list(l1, l2)
> res = lapply(X = list(l1, l2), FUN = test_function)
> res
[[1]]
[1] "a = 10 b =  11 \n"

[[2]]
[1] "a = 20 b =  22 \n"

# First element of the results
> res[1]
[1] "a = 10 b =  11 \n"

# Second element of the results
> res[2]
[1] "a = 20 b =  22 \n"