[R] Conditional Correlation
Daniel Malter
daniel at umd.edu
Sat Jun 18 05:19:27 CEST 2011
This question was just answered yesterday in this post:
http://r.789695.n4.nabble.com/Correlations-by-subgroups-td3599548.html#a3600553
One solution:
x<-c(1,1,1,1,1,2,2,2,2,2)
y<-rnorm(10)
z<-y+rnorm(10)
by(data.frame(y,z),factor(x),cor)
HTH,
Daniel
Mateus Rabello wrote:
>
> Hi,
> How can I accomplish this in R. Example:
>
> I have the following data.frame:
>
> data <-
> data.frame(x=c(1,2,3,4,5,6,5,3,7,1,0,4,8),y=c(1,2,1,2,2,2,1,1,1,2,2,2,2),z=c(5,8,4,3,4,1,6,3,3,6,3,5,7))
>
> Supposing that data$y is a factor, I would like to find the Spearman
> correlation between data$x and data$z indexing it by data$y.
> To be more specific, I want to find two correlations: between x and z with
> y==1 and the same correlation with x and z where y==2.
> Something like:
>
> cor(data$x[data$y==1],data$z[data$y==1],method= "spearman") and
> cor(data$x[data$y==2],data$z[data$y==2],method= "spearman"),
>
> but without having to write all the values for data$y and use cor more
> than once.
>
> I hope I made myself clear.
> Thanks
> Mateus
>
>
> [[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.
>
--
View this message in context: http://r.789695.n4.nabble.com/Conditional-Correlation-tp3607080p3607134.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list