Hello, how can I use the function "cor()" with x and y in function "aggregate()" or "by()"? The data are like this: x y group 1 4 B 2 4 B 3 5 C I would like obtain the correlation between x and y for each subset. I don't want to use the workaround with the function subset(), because I have many groups. Thanks in advance. Christfried Kunath KC
Sundar Dorai-Raj
2005-Apr-08 16:46 UTC
correlation by group (was Re: [R] kunamorph@web.de)
Christfried Kunath wrote on 4/8/2005 10:39 AM:> Hello, > > how can I use the function "cor()" with x and y in function > "aggregate()" or "by()"? > > The data are like this: > x y group > 1 4 B > 2 4 B > 3 5 C > > I would like obtain the correlation between x and y for each subset. I > don't want to use the workaround with the function subset(), because I > have many groups. > > Thanks in advance. > Christfried Kunath KC >(Please use an informative subject as the posting guide recommends.) Your example is not very useful, but perhaps you need something like: tmp <- data.frame(x = rnorm(100), y = rnorm(100), group = rep(letters[1:5], each = 20)) sapply(split(tmp, tmp["group"]), function(z) cor(z$x, z$y)) # OR rbind(by(tmp, tmp["group"], function(z) cor(z$x, z$y))) --sundar
Possibly Parallel Threads
- nls(): Levenberg-Marquardt, Gauss-Newton, plinear - PI curve fitting
- resolving expand.grid & NA errors
- fitted probabilities in multinomial logistic regression are identical for each level
- inconsistent output using 'round'
- Lighttpd Configuration Independent of Hostname or IP