The data is in the attachment.
What I wanna get is:
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 32 11 10 3
Brown 53 50 25 15
Red 10 10 7 7
Blond 3 30 5 8
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 36 9 5 2
Brown 66 34 29 14
Red 16 7 7 7
Blond 4 64 5 8
Then,how to change the dataframe to tables?
Many thanks for your help.
Best
-------------- next part --------------
"hair" "eye" "sex" "freq"
"Black" "Brown" "Male" 32
"Black" "Blue" "Male" 11
"Black" "Hazel" "Male" 10
"Black" "Green" "Male" 3
"Brown" "Brown" "Male" 38
"Brown" "Blue" "Male" 50
"Brown" "Hazel" "Male" 25
"Brown" "Green" "Male" 15
"Red" "Brown" "Male" 10
"Red" "Blue" "Male" 10
"Red" "Hazel" "Male" 7
"Red" "Green" "Male" 7
"Blond" "Brown" "Male" 3
"Blond" "Blue" "Male" 30
"Blond" "Hazel" "Male" 5
"Blond" "Green" "Male" 8
"Black" "Brown" "Female" 36
"Black" "Blue" "Female" 9
"Black" "Hazel" "Female" 5
"Black" "Green" "Female" 2
"Brown" "Brown" "Female" 81
"Brown" "Blue" "Female" 34
"Brown" "Hazel" "Female" 29
"Brown" "Green" "Female" 14
"Red" "Brown" "Female" 16
"Red" "Blue" "Female" 7
"Red" "Hazel" "Female" 7
"Red" "Green" "Female" 7
"Blond" "Brown" "Female" 4
"Blond" "Blue" "Female" 64
"Blond" "Hazel" "Female" 5
"Blond" "Green" "Female" 8
say, 'Dat' is your data frame, then one way to do it is: with(Dat, tapply(freq, list(hair, eye, sex), c)) I hope it helps. Best, Dimitris On 2/18/2011 8:50 AM, Lao Meng wrote:> The data is in the attachment. > > What I wanna get is: > , , Sex = Male > Eye > Hair Brown Blue Hazel Green > Black 32 11 10 3 > Brown 53 50 25 15 > Red 10 10 7 7 > Blond 3 30 5 8 > , , Sex = Female > Eye > Hair Brown Blue Hazel Green > Black 36 9 5 2 > Brown 66 34 29 14 > Red 16 7 7 7 > Blond 4 64 5 8 > > > Then,how to change the dataframe to tables? > > Many thanks for your help. > > Best > > > > ______________________________________________ > 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.-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
This is a built-in dataset in R - see ?HairEyeColor and str() it. I smell homework... Dennis On Thu, Feb 17, 2011 at 11:50 PM, Lao Meng <laomeng.3@gmail.com> wrote:> The data is in the attachment. > > What I wanna get is: > , , Sex = Male > Eye > Hair Brown Blue Hazel Green > Black 32 11 10 3 > Brown 53 50 25 15 > Red 10 10 7 7 > Blond 3 30 5 8 > , , Sex = Female > Eye > Hair Brown Blue Hazel Green > Black 36 9 5 2 > Brown 66 34 29 14 > Red 16 7 7 7 > Blond 4 64 5 8 > > > Then,how to change the dataframe to tables? > > Many thanks for your help. > > Best > > ______________________________________________ > R-help@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]]
Try this: xtabs(Freq ~ Hair + Eye + Sex, Dat) Using Dimitri's Dat example. On Fri, Feb 18, 2011 at 5:50 AM, Lao Meng <laomeng.3@gmail.com> wrote:> The data is in the attachment. > > What I wanna get is: > , , Sex = Male > Eye > Hair Brown Blue Hazel Green > Black 32 11 10 3 > Brown 53 50 25 15 > Red 10 10 7 7 > Blond 3 30 5 8 > , , Sex = Female > Eye > Hair Brown Blue Hazel Green > Black 36 9 5 2 > Brown 66 34 29 14 > Red 16 7 7 7 > Blond 4 64 5 8 > > > Then,how to change the dataframe to tables? > > Many thanks for your help. > > Best > > ______________________________________________ > R-help@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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Maybe Matching Threads
- odds ratios for n-way tables: seeking an *apply-able method
- how to make automatically each level from data.frame to vector
- HSAURtable question
- mca & contingency tables - error: "All variables must be factors"
- plyr: a*ply with functions that return matrices-- possible bug in aaply?