[R] Correlations by subgroups

Daniel Malter daniel at umd.edu
Wed Jun 15 22:58:34 CEST 2011


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


jfdawson wrote:
> 
> I'm hoping there is a simple answer to this - it seems that there should
> be, but I can't figure it out.
> 
> I have a matrix/data frame with three variables of interest - V1, V2, V3.
> One, V1, is a factor with x levels (x may be a large number); I want to
> calculate  the correlation between the other two (i.e. cor(V2,V3)) for
> each level, and store it as a vector of length x.
> 
> I should think this should be possible using a function like tapply, but I
> cannot work out what the syntax would be - everything I have tried
> produces either an error, or just repeats the correlations between V2 & V3
> on the whole matrix.
> 
> Could anyone suggest what I should be doing?
> 
> Many thanks,
> Jeremy
> 

--
View this message in context: http://r.789695.n4.nabble.com/Correlations-by-subgroups-tp3599548p3600553.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list