[R] turn list into dataframe

R. Michael Weylandt michael.weylandt at gmail.com
Tue Oct 9 00:19:50 CEST 2012


On Mon, Oct 8, 2012 at 10:49 PM, Benjamin Caldwell
<btcaldwell at berkeley.edu> wrote:
> Dear R users,
>
> I'm starting to use 'apply' functions rather than for loops in R, and
> sometimes the output is a bit different than what I want. In this case, the
> command was
>
>
> tapply(myvector,myindex,cumsum)
>
> And the output was something like this:
>
> $`SNRL1 Core 120`
> [1]  2.8546  4.0778  5.2983  6.3863  7.5141  8.5498  9.5839 10.6933
>
> $`SNRL1 Core 230`
> [1]  7.6810  8.7648  9.8382 10.8903 11.9840 13.0541 14.1132 15.1657
>
> $`VAL 1.1.NA.na30`
>  [1] 22.0840 30.3001 35.2505 42.8085 48.5220 52.0604 57.9428 61.4930
> 64.4550 67.3543 69.8435 72.9508 74.4730 76.3104
>
> $`VAL 1.2.NA.na15`
>  [1] 33.8895 38.7440 41.0536 44.1581 46.4891 48.3130 51.0973 52.9241
> 54.6404 56.1265 57.5064 59.0745
>
> $`VAL 1.2.NA.na30`
>  [1]  6.6408 10.6838 13.8328 15.5435 18.3037 20.3315 22.8817 24.4481
> 26.4106 27.6658 29.6455 30.8490 31.8680
>
> $`VAL 1.3.NA.na10`
>  [1]  4.8198  7.1274  8.9536 11.5954 14.0845 15.5116 16.9462 18.1269
> 19.3453 20.5723 21.7122 22.8643
>
> $`VAL 1.3.NA.na20`
> [1]  5.7382  8.2056  9.4489 10.8225 12.3497 13.6879 15.1077 16.3229
>
> ,
> That's fine, but I need the output as a dataframe. I'm not even sure what
> to call this list, but it has multiple entries for each item.
>
> Forgive the fact that I don't have the data for you to use, I'm wondering
> if anyone knows about a pre-existing function that will allow be to turn
> the above list form into a dataframe. Thanks
>
>
> *Ben Caldwell*
>

Hi Ben,

In the future, you can get some good advice for making reproducible
examples here:

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

In your case, I think something like

do.call(data.frame, LIST)

will work. Take a look at ?do.call for an explanation of the magic.

Cheers,
Michael




More information about the R-help mailing list