Create a list from a list without nesting

后端 未结 2 1057
灰色年华
灰色年华 2021-01-29 07:15

Lets say I have a list: x <- list(mtcars, iris, cars)

Now lets say I want to add another dataset to the list. Adding one to the end is easy. x[[4]]

2条回答
  •  长情又很酷
    2021-01-29 07:17

    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
    #...
    

    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)
    

    ...which means you can specify exactly where you want df added in the order of lists.

提交回复
热议问题