将列表元素转换为数据框列

时间:2018-12-13 08:53:03

标签: reshape

创建列表:

my <- list(Low=c("21.1","126.8","78.3","32.4","34.5"), 
High = ("301.3","17.3","20.3" ))

显示列表输出:

my

$`Low`
[1] "21.1"  "126.8" "78.3"  "32.4"  "34.5" 

$High
[1] "301.3" "17.3"  "20.3" 

转换为数据框:

as.data.frame(my)

获取错误:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 5, 3

我想要的输出是:两列数据框。

Low      
21.1           
126.8                
78.3                 
32.4       
34.5

High        
301.3        
17.3          
20.3

1 个答案:

答案 0 :(得分:1)

以下内容将满足您的要求

my <- list(Low=c("21.1","126.8","78.3","32.4","34.5"),
High = c("301.3","17.3","20.3" ))

n.obs <- sapply(my, length)

seq.max <- seq_len(max(n.obs))

my_df <- data.frame(sapply(my, "[", i = seq.max))

names(my_df) = names(my)

print(my_df)