从列表创建列表而不嵌套 [英] Create a list from a list without nesting
问题描述
我说我有一个列表:x <- list(mtcars, iris, cars)
现在可以说我想将另一个数据集添加到列表中.在末尾添加一个很容易. x[[4]] <- df
Now lets say I want to add another dataset to the list. Adding one to the end is easy. x[[4]] <- df
但是,假设我要在开头添加一个元素,或者说要将两个列表合并为一个列表.我不能做这样的事情
But let's say I want to add an element to the beginning or let's say I want to combine two lists into one list. I cannot do something like this
list(df,x) # not equal to list(df, mtcars, iris, cars)
使用c
可以使用,但是上面的代码会给我一个列表,该列表嵌套了第二个元素.
With c
this would work, but the above would give me a list with a list nested as the second element.
是否可以从列表中创建列表而不进行嵌套?
Is there to create a list from a list without nesting?
推荐答案
您需要具有list
才能添加到其他list
中,因此:
You need to have a list
to add to the other list
s, so:
df <- data.frame(a=1:10)
c(list(df), x)
#List of 4
#...
您也可以使用append
,但是您需要再次传递list
:
You can use append
too, but you need to pass a list
again:
append(list(df),x)
# equivalent to:
append(x, list(df), after=0)
...这意味着您可以按照列表的顺序确切地指定要添加df
的位置.
...which means you can specify exactly where you want df
added in the order of lists.
这篇关于从列表创建列表而不嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!