Yi
2012-Aug-29 02:01 UTC
[R] Help on calculating spearman rank correlation for a data frame with conditions
Dear all, Suppose my data frame is as follows: id price distance 1 2 4 1 3 5 ... 2 4 8 2 5 9 ... n 3 7 n 8 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. Then I tried to use apply(data,list='id',cor(price , distance , method = "spearman")) to [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Aug-29 04:20 UTC
[R] Help on calculating spearman rank correlation for a data frame with conditions
On Tue, Aug 28, 2012 at 9:01 PM, Yi <liuyi.feier at gmail.com> wrote:> Dear all, > > Suppose my data frame is as follows: > > id price distance > 1 2 4 > 1 3 5 > ... > 2 4 8 > 2 5 9 > ... > n 3 7 > n 8 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. > > Then I tried to use > apply(data,list='id',cor(price , distance , method = "spearman")) > to >You seem to have been cut off mid-thought, but I'm guessing you want something more like: tapply(data, data$id, function(x), cor(x$price, x$distance, method "spearman")) Cheers, Michael> [[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.
arun
2012-Aug-29 05:16 UTC
[R] Help on calculating spearman rank correlation for a data frame with conditions
Hi, Try this: dat1<-read.table(text=" id? price? distance 1? 2??? 4 1? 3??? 5 2? 4? 8 2? 5? 9 3? 6? 3 3? 4? 8 ",sep="",header=TRUE) dat2<-split(dat1,dat1$id) ?lapply(dat2,function(x) cor(x[2],x[3],method="spearman")) A.K. ----- Original Message ----- From: Yi <liuyi.feier at gmail.com> To: R-help at r-project.org Cc: Sent: Tuesday, August 28, 2012 10:01 PM Subject: [R] Help on calculating spearman rank correlation for a data frame with conditions Dear all, Suppose my data frame is as follows: id? price? distance 1? 2? ? 4 1? 3? ? 5 ... 2? 4? 8 2? 5? 9 ... n? 3? 7 n? 8? 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. Then I tried to use apply(data,list='id',cor(price , distance , method = "spearman")) to ??? [[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.
S Ellison
2012-Aug-29 15:45 UTC
[R] Help on calculating spearman rank correlation for a data frame with conditions
> 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}}