[R] computation on a table

Claus Atzenbeck claus.atzenbeck at freenet.de
Sat Nov 12 23:41:33 CET 2005


Hello,

I have a table (1) of the form

      q1 q3 q4 q8 q9
    A  5  2  0  1  3
    B  2  0  2  4  4

I have another table (2):

      q1 q2 q3 q4 q5 q6 q7 q8 q9
    C 10  7  4  2  6  9  3  1  2

I would like to divide the numbers in table (1) by the number of the
appropriate column in table (2):

         q1   q3   q4   q8   q9
    A  5/10  2/4  0/2  1/1  3/2
    B  2/10  0/4  2/2  4/1  4/2

The result would look lie this:

         q1   q3   q4   q8   q9
    A   0.5  0.5    0    1  1.5
    B   0.2    0    1    4    2

BACKGROUND: I have a data frame with measured times for answering
questions. I want to know how many PERCENT of the answers are wrong,
caused by reason A or B.

This gives me the subset of false answers. The table looks like table (1):

    fail <- subset(questions, type=="wrong")
    fail$qid <- factor(fail$qid)
    failtab <- table(fail$failtype, fail$qid)

The following gives me information about how often a specific question
was asked. This is similar to table (2) above.

    count <- table(questions$failtype, questions$qid)
    count <- colSums(count)

One solution would be to delete the line that calls factor(...) on the
subset and calculate failtab/count. However, then I have the problem
that I have to get rid of all columns of the table that have '0' in all
rows.

Thanks for any hint.
Claus




More information about the R-help mailing list