[R] calculation fraction/ratio

2008-02-02 Thread YIHSU CHEN
Dear R users I wonder if there is a quick way to calculate the ratio/fraction of a list/data frame. For example, if I have a data frame with two fields: Index and A. I would like to know the fractions of A's within the same Index. That is, for Index =1, three fractions will be 1/(1+2+3)=0.17,

Re: [R] calculation fraction/ratio

2008-02-02 Thread jim holtman
Is this what you want? x - read.table(textConnection(Index A + 1 1 + 1 2 + 1 3 + 2 4 + 2 3 + 3 7 + 3 9 + 3 3 + 3 1), header=TRUE) data.frame(x, value=ave(x$A, x$Index, FUN=function(z) z / sum(z))) Index A value 1 1 1 0.167 2 1 2 0.333 3 1 3 0.500 4 2 4