1
我想將可變大小的列表平鋪到單個數據框中。除了每個列表可以具有不同數量的元素之外,這很容易。R - 將可變大小的列表展平到數據框?
下面是一個更好地描述問題的例子。有兩場音樂會。第一場音樂會有兩個樂隊。第二場音樂會只有一個樂隊。
> concert1 <- list(bands=list(band=list("Foo Fighters","Ace of Base"), venue="concert hall 1"))
> concert2 <- list(bands=list(band=list("The Black Keys"), venue="concert hall 2"))
> concertsList <- list(concert1=concert1, concert2=concert2)
> str(concertsList)
List of 2
$ concert1:List of 1
..$ bands:List of 2
.. ..$ band :List of 2
.. .. ..$ : chr "Foo Fighters"
.. .. ..$ : chr "Ace of Base"
.. ..$ venue: chr "concert hall 1"
$ concert2:List of 1
..$ bands:List of 2
.. ..$ band :List of 1
.. .. ..$ : chr "The Black Keys"
.. ..$ venue: chr "concert hall 2"
我想將'concertsList'拼成一個看起來像下面這樣的數據框。
> data.frame(concert=c("concert1","concert2"), band.1=c("Foo Fighers", "The Black Keys"), band.2=c("Ace of Base", NA), venues=c("concert hall 1", "concert hall 2"))
concert band.1 band.2 venues
1 concert1 Foo Fighers Ace of Base concert hall 1
2 concert2 The Black Keys <NA> concert hall 2
您的想法將不勝感激。