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.1666667 2 1 2 0.3333333 3 1 3 0.5000000 4 2 4 0.5714286 5 2 3 0.4285714 6 3 7 0.3500000 7 3 9 0.4500000 8 3 3 0.1500000 9 3 1 0.0500000
On Feb 1, 2008 6:19 PM, YIHSU CHEN <[EMAIL PROTECTED]> wrote: > 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", > "2/(1+2+3)=0.33", and "3/1+2+3=0.5". Likewise for Index =2 and Index 3. > So, I then generate a new vector of 0.17, 0.33, 0.5... ,etc. > > > Index A 1 1 1 2 1 3 2 4 2 3 3 7 3 9 3 3 3 1 > > Thank you so much > > Yihsu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.