我想將一個串列變成一個資料框架,其中每個名稱(listoflists)是一個不同的列名。這是一個非常大的串列,所以我想以最有效的方式完成這個任務。
listoflists=list(c(1。 2, 3。 4)。 c(5。 6, 7。 8))
names(listoflists) < - c("col1"/span>。 "col2")
我怎么做才能得到以下結果:
我怎么做才能得到以下結果?
print(df)
col1 col2
1 5
2 6
3 7
4 8
uj5u.com熱心網友回復:
我們可以用data.frame
data.frame(listoflists)
輸出
col1 col2
1 1 5
2 2 6
3 3 7
4 4 8
如果OP的意思是,它作為一個嵌套的串列,那么unlist內部的串列元素通過回圈lapply,并用data.frame
data. frame(lapply(listoflists,unlist)
如果這應該是最有效的方式,那么也可以應用setDT
library(data.table)
setDT(listoflists)
輸出
>/span> listoflists
col1 col2
1: 1 5
2: 2 6
3: 3 7
4: 4 8
uj5u.com熱心網友回復:
library(dplyr)
bind_rows(listoflists)
# A tibble: 4 x 2
col1 col2
<dbl> <dbl>
1 1 5
2 2 6
3 3 7
4 4 8
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332205.html
標籤:
