[R] Table of Summaries

David Winsemius dwinsemius at comcast.net
Wed Jun 3 15:12:58 CEST 2009


On Jun 2, 2009, at 11:01 PM, milton ruser wrote:

> Hi there,
>
> It is not so many ellegant, but can works.
>
> df<-read.table(stdin(), head=T, sep=",")
> Name,Fraction
> Harry,335/335
> Harry,124/506
> Harry,124/506
> Dick,133
> Tom,335/335
> Tom,335/335

At this point one could simply used:

 >  table(df)
        Fraction
Name    124/506 133 335/335
   Dick        0   1       0
   Harry       2   0       1
   Tom         0   0       2

Since a table is really just  a pretty matrix, one can reorder the  
rows to fit the initial specs:

 >  table(df)[order(c(2,3,1)),]
        Fraction
Name    124/506 133 335/335
   Tom         0   0       2
   Dick        0   1       0
   Harry       2   0       1

>
> df.freq<-data.frame(table(df))
> df.freq


 >  table(df)[order(c(2,3,1)),]
        Fraction
Name    124/506 133 335/335
   Tom         0   0       2
   Dick        0   1       0
   Harry       2   0       1

I never understood how to get reshape to give me what I want, which  
undoubtedly says more about me than that perfectly behaved function.  
Nonetheless I usually end up using xtabs for the task of cross- 
classifying with a frequency weight. Its formula convention "makes  
more sense" to me:

 >  xtabs(Freq ~ Name + Fraction, df.freq)
        Fraction
Name    124/506 133 335/335
   Dick        0   1       0
   Harry       2   0       1
   Tom         0   0       2

 >  xtabs(Freq ~ Name + Fraction, df.freq)[order(c(2,3,1)),]
        Fraction
Name    124/506 133 335/335
   Tom         0   0       2
   Dick        0   1       0
   Harry       2   0       1

-- 
Regards;
David Winsemius

>
> df.freq.wide<-reshape(df.freq, v.names="Freq", idvar="Name",
> timevar="Fraction", direction="wide")
> df.freq.wide
>
> cheers
>
> milton
> brazil=toronto
>
>
>
> On Tue, Jun 2, 2009 at 7:41 PM, sedm1000 <gdoran at mit.edu> wrote:
>
>>
>> Apologies for the novice question, but this is likely beyond my  
>> google
>> searching range..
>>
>> I am trying to create a table from the summaries of a file. The 2  
>> column
>> data table looks like;
>>
>> Harry  335/335
>> Harry  124/506
>> Harry  124/506
>> Dick   133
>> Tom    335/335
>> Tom    335/335
>>
>> with 1000 unique values in col 1, 300 unique in col 2. The  'summary'
>> doesn't seem to handle this many values well.
>>
>> I would like to summarise this data in a table that reads the  
>> number of
>> occurences of the values of col 2  for each unique name in col 1...
>>
>> Along the lines...
>>
>>         335/335  124/506  133
>> Tom        2            0            0
>> Dick        0            0            1
>> Harry       1            2            0
>>
>> I hope I can trouble someone for their expert advice on what is  
>> likely a
>> trivial matter...
>> Many thanks.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list