Converting list of lists into dataframes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting list of lists into dataframes

xhan
Hi,

I have a list of lists that I would like to convert into a dataframe such that the name(?) of the individual lists is replicated as rows with each item in the list listed in another column.

I have provided a partial from my list: (let's say the list is called "help.me")

[[2]]
  [1] 18 27 11 18 12 15 12 14  7 17 15 16 12 19 14 21 13 13 20 20 16 15 18 11 14
 [26] 10 11 15  9 16 12 17 20 12 11 15 15 16 14 14 12 15 14 11 19 12 18 24 12  8
 [51] 14 13 13 15 16  9 13 11 17  8  8 15 18 11 12 16 12 12 12 13 12 25 21 11  8
 [76] 19  5 10 13 16 11 13 11 16  7 12 15 15 16 11  9 10 15  7 16 12 11 13 12 12

[[3]]
  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16 15 20 14 14
 [26] 11 11 16  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13 18 25 13 10
 [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 16 12 26 22 11  8
 [76] 21  6 10 14 18 11 14 11 16  8 14 17 16 17 11 10 10 16  7 20 13 11 13 12 14

[[4]]
  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16 15 20 14 14
 [26] 11 11 17  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13 18 25 13 10
 [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 17 12 26 22 11  8
 [76] 21  6 10 14 18 11 14 11 16  8 14 17 17 17 11 10 10 16  7 20 13 11 13 12 14

What I would like is two columns:
Column1                Column2
2                          18
2                          27
2                          11
2                          18
2                          12
2                          15
.                           .
.                           .
.                           .
3                          18
3                          27
3                          12
.                           .
.                           .
.                           .

And so forth. I would appreciate any help, thanks so much!

Shirley~
Reply | Threaded
Open this post in threaded view
|

Re: Converting list of lists into dataframes

djmuseR
In the absence of a reproducible example (your example is not
reproducible as is), try this:

names(help.me) <- as.character(2:4)

library('plyr')
newDF <- ldply(help.me, rbind)
newDF[['.id']] <- as.numeric(newDF[['.id']])

ldply will create a new column named .id that contains the name of the
list component (hence the choice of names). The post-processing to
convert the characters to numeric values should get you the result you
want.

HTH,
Dennis

On Mon, Oct 17, 2011 at 2:11 PM, xhan <[hidden email]> wrote:

> Hi,
>
> I have a list of lists that I would like to convert into a dataframe such
> that the name(?) of the individual lists is replicated as rows with each
> item in the list listed in another column.
>
> I have provided a partial from my list: (let's say the list is called
> "help.me")
>
> [[2]]
>  [1] 18 27 11 18 12 15 12 14  7 17 15 16 12 19 14 21 13 13 20 20 16 15 18
> 11 14
>  [26] 10 11 15  9 16 12 17 20 12 11 15 15 16 14 14 12 15 14 11 19 12 18 24
> 12  8
>  [51] 14 13 13 15 16  9 13 11 17  8  8 15 18 11 12 16 12 12 12 13 12 25 21
> 11  8
>  [76] 19  5 10 13 16 11 13 11 16  7 12 15 15 16 11  9 10 15  7 16 12 11 13
> 12 12
>
> [[3]]
>  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16 15 20
> 14 14
>  [26] 11 11 16  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13 18 25
> 13 10
>  [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 16 12 26 22
> 11  8
>  [76] 21  6 10 14 18 11 14 11 16  8 14 17 16 17 11 10 10 16  7 20 13 11 13
> 12 14
>
> [[4]]
>  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16 15 20
> 14 14
>  [26] 11 11 17  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13 18 25
> 13 10
>  [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 17 12 26 22
> 11  8
>  [76] 21  6 10 14 18 11 14 11 16  8 14 17 17 17 11 10 10 16  7 20 13 11 13
> 12 14
>
> What I would like is two columns:
> Column1                Column2
> 2                          18
> 2                          27
> 2                          11
> 2                          18
> 2                          12
> 2                          15
> .                           .
> .                           .
> .                           .
> 3                          18
> 3                          27
> 3                          12
> .                           .
> .                           .
> .                           .
>
> And so forth. I would appreciate any help, thanks so much!
>
> Shirley~
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Converting-list-of-lists-into-dataframes-tp3913433p3913433.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting list of lists into dataframes

David Winsemius
In reply to this post by xhan

On Oct 17, 2011, at 5:11 PM, xhan wrote:

> Hi,
>
> I have a list of lists that I would like to convert into a dataframe  
> such
> that the name(?) of the individual lists is replicated as rows with  
> each
> item in the list listed in another column.
>
> I have provided a partial from my list: (let's say the list is called
> "help.me")

Actually only a list of numeric vectors:

new.df <- stack(as.data.frame(help.me))

--
avid

>
> [[2]]
>  [1] 18 27 11 18 12 15 12 14  7 17 15 16 12 19 14 21 13 13 20 20 16  
> 15 18
> 11 14
> [26] 10 11 15  9 16 12 17 20 12 11 15 15 16 14 14 12 15 14 11 19 12  
> 18 24
> 12  8
> [51] 14 13 13 15 16  9 13 11 17  8  8 15 18 11 12 16 12 12 12 13 12  
> 25 21
> 11  8
> [76] 19  5 10 13 16 11 13 11 16  7 12 15 15 16 11  9 10 15  7 16 12  
> 11 13
> 12 12
>
> [[3]]
>  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16  
> 15 20
> 14 14
> [26] 11 11 16  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13  
> 18 25
> 13 10
> [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 16 12  
> 26 22
> 11  8
> [76] 21  6 10 14 18 11 14 11 16  8 14 17 16 17 11 10 10 16  7 20 13  
> 11 13
> 12 14
>
> [[4]]
>  [1] 18 27 12 18 18 17 12 16  7 17 16 18 14 19 16 22 13 14 22 20 16  
> 15 20
> 14 14
> [26] 11 11 17  9 16 12 19 20 12 11 17 15 17 14 14 14 15 15 11 19 13  
> 18 25
> 13 10
> [51] 14 13 14 15 18 10 13 11 18 12 10 15 20 13 12 17 13 13 12 17 12  
> 26 22
> 11  8
> [76] 21  6 10 14 18 11 14 11 16  8 14 17 17 17 11 10 10 16  7 20 13  
> 11 13
> 12 14
>
> What I would like is two columns:
> Column1                Column2
> 2                          18
> 2                          27
> 2                          11
> 2                          18
> 2                          12
> 2                          15
> .                           .
> .                           .
> .                           .
> 3                          18
> 3                          27
> 3                          12
> .                           .
> .                           .
> .                           .
>
> And so forth. I would appreciate any help, thanks so much!
>
> Shirley~
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Converting-list-of-lists-into-dataframes-tp3913433p3913433.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.