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

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Jun 17 03:45:55 CEST 2010

Is this the kind of thing you want?

> 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')

> test <- data.frame(test)
> tab <- with(test, table(paste(leave, arrive, sep=""), line))
> tab <- tab/rowsums(tab)
> rowsums(tab^2)
   ab    ef 
0.625 0.500 

Your 'ab' calculation I think may not be quite correct. 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of YI LIU
Sent: Thursday, 17 June 2010 11:28 AM
To: r-help at r-project.org
Subject: [R] how to 'average' one col wrt to another one

Hi, folks,


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?


	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list