[R] Calculating proportions from a data frame rather than a table

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Oct 3 23:20:24 CEST 2007


On 10/3/07, Farrel Buchinsky <fjbuch at gmail.com> wrote:
> Your solution would work if the data frame contained the raw data. In that
> case the table function as you outlined would be a table crossing all the
> levels of column 1 with all the levels of column 2.
> Instead my data frame is the table. It is an aggregate table (I may be using
> the wrong buzzwords here).

Does this help:

> foo <- Titanic[,2,2,]
> foo
      Survived
Class  No Yes
  1st   4 140
  2nd  13  80
  3rd  89  76
  Crew  3  20
> bar <- as.data.frame.table(foo)
> bar
  Class Survived Freq
1   1st       No    4
2   2nd       No   13
3   3rd       No   89
4  Crew       No    3
5   1st      Yes  140
6   2nd      Yes   80
7   3rd      Yes   76
8  Crew      Yes   20
> xtabs(Freq ~ Class + Survived, data = bar) # compare with foo
      Survived
Class   No Yes
  1st    4 140
  2nd   13  80
  3rd   89  76
  Crew   3  20

-Deepayan



More information about the R-help mailing list