Hi Yi, Try this:
res <- with(data.frame(test), prop.table(table(paste(leave, arrive, sep = ""), line), 1)) rowSums(res^2) HTH, Jorge On Wed, Jun 16, 2010 at 9:28 PM, YI LIU <> wrote: > Hi, folks, > > test=matrix(rep(letters[1:3],6),nrow=6,byrow=T) > test[2,]=letters[5:7] > test[4,]=c('e','f','i') > test[1,3]='i' > colnames(test)=c('leave','arrive','line') > > The code will generate a sample dataset. I have thousands rows of data. > > For the same 'leave' and 'arrive', how can I get the sum of square of > percentages of 'line' for each combination of 'leave' and 'arrive'? > > For the sample dataset, we have 5 combinations of 'leave' and 'arrive', > namely, 'ab', 'ef' > > For 'ab', the line can be 'c' or 'i'. 1st row, it is line 'i' (1/3), and > 5&6th row (2/3), it is line 'c'. then the sum of square of percentage for > 'ab' is (1/3)^2+(2/3)^2=0.56 > > For 'ef', it is 0.5^2+0.5^2=0.5 > > I would like the final dataset to be like as follows: > > ab 0.56 > ef 0.5 > .... > > How to achieve it? > > Thanks > > [[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. > [[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.