Create a list from a list without nesting

后端 未结 2 1051
灰色年华
灰色年华 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.

    0 讨论(0)
  • 2021-01-29 07:27

    Yes, you use the append function. For example:

    x <- list()
    
    x <- append(x = x, c(mtcars, iris, cars))
    
    0 讨论(0)
提交回复
热议问题