Ana
Ana

Reputation: 11

Calculating percentages from xtabs by three variables

I have these variables:

Each participant completed at least 6 examples of each sentence type (in total 96 sentences each).

I would like to calculate the percentages of the 3 choices for each group for each sentence type.

The data were gathered to a table I created in calc. I saved it as .csv file and I imported it in R.

I have created three-way xtabs. Below it is shown a part of the xtab for one sentence type:

three_way3 <- xtabs(~language + response_aspect + sentence type, data=teliko_eikones)
addmargins(three_way3, 2)

 sentence type = while

      response_aspect
Language No Aspect Imperfective     Perfective      Sum
    GR   9           397           2        408
    RO   9           170           1        180
    RU   7          183            2        192

I would like to do something like that. I don’t know how to transform the raw frequencies to percentages for each sentence type.

      response_aspect
Language No Aspect Imperfective     Perfective      Sum
    GR   2.2          97.3             0.4         100
    RO   0           100           0        100
    RU   0          0              100      100

Upvotes: 1

Views: 182

Answers (1)

jay.sf
jay.sf

Reputation: 72778

You can easily apply proportions() on xtabs output.

> xtabs(cbind(ncases, ncontrols) ~ agegp + alcgp, data = dat) |> proportions()
, ,  = ncases

       alcgp
agegp     0-39g/day       40-79      80-119        120+
  25-34 0.000000000 0.000000000 0.000000000 0.003174603
  35-44 0.003174603 0.012698413 0.000000000 0.012698413
  45-54 0.000000000 0.000000000 0.000000000 0.000000000
  55-64 0.000000000 0.000000000 0.000000000 0.000000000
  65-74 0.000000000 0.000000000 0.000000000 0.000000000
  75+   0.000000000 0.000000000 0.000000000 0.000000000

, ,  = ncontrols

       alcgp
agegp     0-39g/day       40-79      80-119        120+
  25-34 0.193650794 0.142857143 0.015873016 0.012698413
  35-44 0.279365079 0.241269841 0.063492063 0.019047619
  45-54 0.000000000 0.000000000 0.000000000 0.000000000
  55-64 0.000000000 0.000000000 0.000000000 0.000000000
  65-74 0.000000000 0.000000000 0.000000000 0.000000000

Note, that you can set specific margins,

proportions(x, margin = NULL)

where 1 denotes rows, 2 columns, and 1:2 both. Read the docs ?proportions.


Data:

> dat <- subset(esoph, agegp %in% c('25-34', '35-44'))

Upvotes: 2

Related Questions