从列表创建列表而不嵌套 [英] Create a list from a list without nesting

查看:69
本文介绍了从列表创建列表而不嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说我有一个列表: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 lists, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆