[R] Problem: using cor.test with by( )

From: Che-hsu (Joe) Chang <chjchang_at_gmail.com>
Date: Sun, 30 Mar 2008 13:11:13 -0400

Hello everyone,  

I'm a new R user switching from SAS and JMP. In the first few days, I have been trying to do a fairly simple task but yet found no success. I've checked the help archive as well as few R textbooks but didn't seem to find the answer. So, please help me if you can.  

Basically, I want to calculate the correlation between variable A and B for every subject in my study. (yep, that simple)

What I did is this:  

by(data, id, function (x) cor.test(A,B, data=x))  

The results gave me numbers of correlation for each subject. But, the problem is that, all these correlations are the same numbers and the sample size was always the entire database (including all subjects). I've also tried the lm function instead of the cor.test, and the by() function works fine. Can any of you tell me what I did wrong? Or could you tell me what is the best way to apply a function by subjects? Thank you!    


Che-hsu (Joe) Chang, Sc.D., P.T.  

        [[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. Received on Sun 30 Mar 2008 - 17:18:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 30 Mar 2008 - 22:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive