Help on calculating spearman rank correlation for a data frame with conditions
On 2012-08-29 08:45, S Ellison wrote:
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")
It may seem a bit of overkill, but the plyr package is handy and gives a nice output: library(plyr) ddply(d, .(g), summarize, correlation = cor(x, y, method = 's')) Peter Ehlers