# [R] how to 'average' one col wrt to another one

From: YI LIU <liuyi.feier_at_gmail.com>
Date: Wed, 16 Jun 2010 18:28:24 -0700

Hi, folks,

```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_at_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. Received on Thu 17 Jun 2010 - 01:31:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jun 2010 - 02:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.