[R] Help on calculating spearman rank correlation for a data frame with conditions

S Ellison S.Ellison at LGCGroup.com
Wed Aug 29 17:45:14 CEST 2012


> id  price  distance
> 1   2     4
> 1   3    5
> ...
> 2  4   8
> 2  5   9
> I would like to calculate the rank-order correlation between 
> price and distance for each id.
> 
> cor(price,distance,method = "spearman") calculate a 
> correlation for all.
> 

Try by()

#Example
d <- data.frame(g=gl(5, 10), x=rnorm(50), y=rnorm(50))
by(d[,2:3], d$g, cor, method="spearman")
 

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}




More information about the R-help mailing list