简体   繁体   English

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

[英]How to create a vector of lists in R?

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. 现在结果向量的长度为2,因为列表的第一个条目(“op”)与tmpList分开。 Is it possible to append the complete tmpList into this vector? 是否可以将完整的tmpList附加到此向量中?
I already tried it with 我已经试过了

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. 它给出了一个长度为1的向量,但是使用列表周围的这个额外列表来访问列表的元素非常麻烦。 (Too much list in one sentence I think.) (我认为,在一个句子中列出的太多了。)

Any help would be appreciated, 任何帮助,将不胜感激,
Martin 马丁

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等提取。

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的向量,它返回一个列表(通过coersion),因为向量中的项必须都是相同的模式(数据类型)。

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"

I think a list() of list() is what you want. 我认为list()list()就是你想要的。

Here is a simple example with lapply() which shows how to use them. 这是lapply()一个简单示例,它展示了如何使用它们。 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()将应用提供给参数中给出的列表的每个元素的函数,并返回包含各个执行结果的列表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM