Alex Rivera | Logout

Convert list to data frame while keeping list-element names

Asked 2012-09-28T11:52:53.193
19

I have list where the elementnames are ID-tags and contains a vector with numeric values. These are of unequal(!) length.

I want to transform it to a data frame where I have the ID in one column and the numeric values in another column. E.g.:

$`1`  
[1] 1 2   
$`2`  
[1] 1 2 3 
$`3`  
[1] 1   

To:

ID   Obs  
1    1  
1    2
2    1
2    2
2    3
3    1
Edit
Report

1 Answer

10

Use reshape2 and melt which has a method melt.list

.list <- list(`1` = 1:2, `2` = 1:3, `3` = 1:2)
library(reshape2)
melt(.list)
##   value L1
## 1     1  1
## 2     2  1
## 3     1  2
## 4     2  2
## 5     3  2
## 6     1  3
## 7     2  3
answered 2012-09-28T11:56:12.860

Your Answer