[R] constructing a data frame from ftable

Phil Spector spector at stat.berkeley.edu
Wed Jun 23 00:25:22 CEST 2010


Erin -
    Perhaps this reproducible example will help:

df = data.frame(a=sample(1:20,10000,replace=TRUE),
                 b=sample(c('M','F'),10000,replace=TRUE),
                 c=sample(LETTERS[1:8],10000,replace=TRUE))
tt = as.data.frame(table(df))
tt$bc = paste(tt$b,tt$c,sep='.')
tt$b=NULL
tt$c=NULL
answer = reshape(tt,idvar='a',timevar='bc',direction='wide')

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Tue, 22 Jun 2010, Erin Hodgess wrote:

> Dear R People:
>
> I have the following data set with the columns DATE, GENDER, and Co.
> Co has 8 possible options.
>
>
>
>> a.df[1:10,]
>         DATE GENDER             Co
> 1  2009-04-16      F           Rash
> 2  2009-04-16      F          Other
> 3  2009-04-16      M      Botulinic
> 4  2009-04-16      M          Other
> 5  2009-04-16      M Constitutional
> 6  2009-04-16      F          Other
> 7  2009-04-16      M          Other
> 8  2009-04-16      M          Other
> 9  2009-04-16      F          Other
> 10 2009-04-16      F          Other
>
>
> Using ftable, I created a table by Date, Co and Gender, which is great.
>
>
>
>
>> ftable(a.df$DATE,a.df$Co,a.df$GENDER)
>                                 F     M
>
> 2009-04-16 Botulinic           622   631
>           Constitutional     1161  1124
>           Gastrointestinal   1020  1029
>           Hemorrhagic         472   477
>           Neurological        812   777
>           Other              6782  6702
>           Rash                607   630
>           Respiratory         962  1008
> 2009-04-17 Botulinic           690   626
>           Constitutional     1088  1119
>           Gastrointestinal   1009   984
>           Hemorrhagic         509   482
>           Neurological        735   794
>           Other              6682  6722
>           Rash                613   599
>           Respiratory         970   931
> 2009-04-18 Botulinic           519   519
>           Constitutional      953   964
>           Gastrointestinal    831   898
>           Hemorrhagic         416   394
>           Neurological        613   693
>           Other              5770  5749
>           Rash                517   522
>           Respiratory         794   822
> 2009-04-19 Botulinic           563   594
>           Constitutional     1039   998
>           Gastrointestinal    882   946
>           Hemorrhagic         449   451
>           Neurological        804   729
>           Other              6022  6037
>           Rash                545   529
>           Respiratory         920   877
>
> However, (because nothing would ever be that simple), I would like to
> create a data frame from the ftable output.
>
> The data frame would have 17 columns; the first is the date, and the
> remaining 16 would be a column for male/Botulinic, female/Botulinic,
> and so on.
>
> Is there a good way to do this, please?
>
> Thanks,
> Erin
>
>
>
> -- 
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list