List to data frame

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

List to data frame

Johannes
Hi,

Any ideas on how to efficiently convert

> list(c(1,2,3),c(4,5,6))

to

> data.frame(OriginalListIndex=c(1,1,1,2,2,2),Item=c(1,2,3,4,5,6))

Thanks for any hints,

Joh

______________________________________________
[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: List to data frame

Henrique Dallazuanna
Try this:

stack(data.frame(list('A' = c(1,2,3), 'B' = c(4,5,6))))

On Mon, Jul 26, 2010 at 11:46 AM, Johannes Graumann <
[hidden email]> wrote:

> Hi,
>
> Any ideas on how to efficiently convert
>
> > list(c(1,2,3),c(4,5,6))
>
> to
>
> > data.frame(OriginalListIndex=c(1,1,1,2,2,2),Item=c(1,2,3,4,5,6))
>
> Thanks for any hints,
>
> Joh
>
> ______________________________________________
> [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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[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: List to data frame

Joshua Wiley-2
In reply to this post by Johannes
Hi,

Here is another option if you already have a list you want to convert.
 This will handle different elements of the list being different
lengths.

#Using your example data
mydata <- list(c(1,2,3),c(4,5,6))

data.frame(
 OriginalListIndex = rep(x = seq_along(mydata),
   times = unlist(lapply(mydata, length))),
 Item = unlist(mydata)
)

#Just to demonstrate that this method works generally
mydata <- list(c(1,2,3), c(7,6), c(3,4,5,6,7,8,9))

data.frame(
 OriginalListIndex = rep(x = seq_along(mydata),
   times = unlist(lapply(mydata, length))),
 Item = unlist(mydata)
)


HTH,

Josh

On Mon, Jul 26, 2010 at 7:46 AM, Johannes Graumann
<[hidden email]> wrote:

> Hi,
>
> Any ideas on how to efficiently convert
>
>> list(c(1,2,3),c(4,5,6))
>
> to
>
>> data.frame(OriginalListIndex=c(1,1,1,2,2,2),Item=c(1,2,3,4,5,6))
>
> Thanks for any hints,
>
> Joh
>
> ______________________________________________
> [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.
>



--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.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.
Reply | Threaded
Open this post in threaded view
|

Re: List to data frame

Johannes
Thanks a lot!

This solves my problem!

Joh

On Monday 26 July 2010 17:06:37 Joshua Wiley wrote:

> Hi,
>
> Here is another option if you already have a list you want to convert.
>  This will handle different elements of the list being different
> lengths.
>
> #Using your example data
> mydata <- list(c(1,2,3),c(4,5,6))
>
> data.frame(
>  OriginalListIndex = rep(x = seq_along(mydata),
>    times = unlist(lapply(mydata, length))),
>  Item = unlist(mydata)
> )
>
> #Just to demonstrate that this method works generally
> mydata <- list(c(1,2,3), c(7,6), c(3,4,5,6,7,8,9))
>
> data.frame(
>  OriginalListIndex = rep(x = seq_along(mydata),
>    times = unlist(lapply(mydata, length))),
>  Item = unlist(mydata)
> )
>
>
> HTH,
>
> Josh
>
> On Mon, Jul 26, 2010 at 7:46 AM, Johannes Graumann
>
> <[hidden email]> wrote:
> > Hi,
> >
> > Any ideas on how to efficiently convert
> >
> >> list(c(1,2,3),c(4,5,6))
> >
> > to
> >
> >> data.frame(OriginalListIndex=c(1,1,1,2,2,2),Item=c(1,2,3,4,5,6))
> >
> > Thanks for any hints,
> >
> > Joh
> >
> > ______________________________________________
> > [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.