[R] computation on a table

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Nov 13 03:26:11 CET 2005


jim holtman <jholtman at gmail.com> writes:

> This will work if you are using matrices (if you have data frames, convert
> them to matrix):
>  > table1
> q1 q3 q4 q8 q9
> A 5 2 0 1 3
> B 2 0 2 4 4
> > table2
> q1 q2 q3 q4 q5 q6 q7 q8 q9
> C 10 7 4 2 6 9 3 1 2
> > index <- match(colnames(table2), colnames(table1), nomatch=0)
> > t(t(table1[,index]) / table2[index != 0, drop=FALSE])
> q1 q3 q4 q8 q9
> A 0.5 0.5 0 1 1.5
> B 0.2 0.0 1 4 2.0

or even

> sweep(table1, 2, table2[colnames(table1)], "/")
   q1  q3 q4 q8  q9
A 0.5 0.5  0  1 1.5
B 0.2 0.0  1  4 2.0


 
> 
>  On 11/12/05, Claus Atzenbeck <claus.atzenbeck at freenet.de> wrote:
> >
> > 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
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> 
> 
> 
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 247 0281
> 
> What the problem you are trying to solve?
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list