[R] how to convert by lists in data.frames

Rui Barradas ruipbarradas at sapo.pt
Tue Oct 9 22:15:47 CEST 2012


You're right, I was in a hurry. This one works.

x <- rnorm(100)
a <- sample(letters[1:4], 100, T)
by.list <- by(x, a, summary)

do.call(rbind, as.list(by.list))


(I would also prefer aggregate.)

Rui Barradas


Em 09-10-2012 19:46, ilai escreveu:
> On Tue, Oct 9, 2012 at 12:25 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>
>> The only wrinkle here (with either rbind or simplify2array) is getting
>> the labels correct if the design is not fully crossed -- i.e. if some
>> groups are missing so that expand.grid() won't work. Then you might
>> have to work harder to extract the information from by.list.
>>
> Agreed. In such a case I might decide to let
>
>   aggregate(breaks~wool+tension, subset(warpbreaks, wool != 'A' | tension !=
> 'H' ), summary)
>
> sort through the headache for me, and overlook the annoying "result is
> actually a matrix put in as a single variable in the data.frame". Personal
> preference maybe but that never made sense to me in the data frame
> construct (even if it is just a list).
> Cheers
>
>
>>> str(by.list)
>> might help here.
>>
>> -- Bert
>>
>> On Tue, Oct 9, 2012 at 11:14 AM, ilai <keren at math.montana.edu> wrote:
>>> On Tue, Oct 9, 2012 at 11:42 AM, Rui Barradas <ruipbarradas at sapo.pt>
>> wrote:
>>>> Hello,
>>>>
>>>> Try
>>>>
>>>> do.call(data.frame, by.list)
>>>>
>>>>
>>> I don't think data.frame inside do.call works in this context. May need
>> it
>>> on the outside to do the job (Only OK here since there is no mixture of
>>> numeric and character/factors in this summary). Something like
>>>
>>> by.list <- by(warpbreaks[, 1], warpbreaks[, -1], summary)
>>> by.dtfrm <- data.frame( do.call( rbind, by.list ) )
>>> by.dtfrm <- cbind( do.call( expand.grid, attr( by.list, 'dimnames' ) ),
>>> by.dtfrm )
>>>
>>>
>>>
>>>
>>> Hope this helps,
>>>> Rui Barradas
>>>> Em 09-10-2012 17:53, Jesus Frias escreveu:
>>>>> Dear R-helpers,
>>>>>
>>>>>
>>>>>
>>>>> I've got a summary of results from a by() call that I am making with a
>>>> list
>>>>> of more than two of factors not very different from the example in the
>>>> by()
>>>>> help page
>>>>>
>>>>>
>>>>>
>>>>> require(stats)
>>>>>
>>>>> by(warpbreaks[, 1],   warpbreaks[, -1],       summary)
>>>>>
>>>>>
>>>>>
>>>>> The result of the command gives a list of the form
>>>>>
>>>>>
>>>>>
>>>>> wool: A
>>>>> tension: L
>>>>>      Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>>>>>     25.00   26.00   51.00   44.56   54.00   70.00
>>>>> ---------------------------------------------------
>>>>> wool: B
>>>>> tension: L
>>>>>      Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>>>>>     14.00   20.00   29.00   28.22   31.00   44.00
>>>>> ---------------------------------------------------
>>>>>
>>>>> .... And so on.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> I would like to convert this result in to a flat data.frame with
>> variable
>>>>> names:
>>>>>
>>>>>
>>>>>
>>>>> Wool, Tension, Min, 1stQ, Median, Mean, 3rdQ, Max
>>>>>
>>>>> A, L , 25.00   26.00   51.00   44.56   54.00   70.00
>>>>> B,  L, 14.00   20.00   29.00   28.22   31.00   44.00
>>>>>
>>>>> ....
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Although I've tried the argument "simplify=T" I haven't been able to
>> get
>>>>> this converted.
>>>>>
>>>>>
>>>>>
>>>>> Is there a simple way to achieve this?
>>>>>
>>>>>
>>>>>
>>>>> Thanks in advance!
>>>>>
>>>>>
>>>>>
>>>>> Regards,
>>>>>
>>>>>
>>>>>
>>>>> Jesus
>>>>>
>>>>>
>>>>>
>>>>> Jesús María Frías Celayeta, PhD
>>>>>
>>>>> Ceann Cúntóir, Scoil Eolaíocht an Bhia agus Sláinte an Chomhshaoil
>>>>>
>>>>> Assistant Head, School of Food Science and Environmental Health,
>>>>>
>>>>> Coláiste Eolaíochtaí agus Sláinte/ College of Sciences and Health,
>>>>>
>>>>> Institiúid Teicneolaíochta Átha Cliath/ Dublin Institute of
>> Technology,
>>>>> Sráid Chathal Brugha, Baile Átha Cliath 1, Éire/Cathal Brugha Street,
>>>> Dublin
>>>>> 1, Ireland
>>>>>
>>>>> F: +353-1-4024459
>>>>>
>>>>> E:  <mailto:james.curtin at dit.ie> jesus.frias at dit.ie
>>>>>
>>>>> W: http://fseh.dit.ie/o4/StaffListing/JesusFrias.html
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Tá an teachtaireacht seo scanta ó thaobh ábhar agus víreas ag Seirbhís
>>>> Scanta Ríomhphost de chuid Seirbhísí Faisnéise, ITBÁC agus meastar í a
>>>> bheith slán.  http://www.dit.ie
>>>>> This message has been scanned for content and viruses by the DIT
>>>> Information Services E-Mail Scanning Service, and is believed to be
>> clean.
>>>> http://www.dit.ie
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>>
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org 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.
>>>>
>>>>          [[alternative HTML version deleted]]
>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org 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.
>>>>
>>>>
>>>          [[alternative HTML version deleted]]
>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>>
>>
>>
>> --
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>>
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>




More information about the R-help mailing list