[R] getting the by() function to work properly

Jancik, Jasper F. jfj4094 at uncw.edu
Mon Oct 23 04:49:23 CEST 2006


Hola everyone,

Solving this issue would be a huge time saver for analyzing my thesis data, so I'll try to be as concise as possible and hope that someone can give me a hand.

I am trying to run a function that I created, which works fine until I try to use it with the by function.  That function is listed at the bottom (f3) just in case that might be the issue.  Essentially I have compiled a large list of data coming from about 30 subjects with 88 trials each and I am trying to get that function to apply to each subject without having to manually attach each subject's data.  The entire filename is 'ss2' so when i run 
>ss2f3=f3(ss2)
>ss2f3
everything is fine.  However when I try to use the by() function:
>by(ss3, sub, f3)
#sub=subject header in the data
I get the error message: " Error in var(x, na.rm = na.rm) : missing observations in cov/cor "
There are no missing observations anywhere in the data so I'm really confused as to how this is happening.  My professor thought it might have something to do with the variance calculations in the function, so when we had it remove bad data the function would run, however it did not calculate everything and it erroneously gave the exact same calculations for each subject.

I even get the message when I try to manually subset the data e.g.:
> s2f3=f3(alldata[sub==2,])
"Error in var(x, na.rm = na.rm) : missing observations in cov/cor"

I hope I've provided all the necessary info (the data set is attached just in case anyone has the time to make it work), but I'd be glad to give more if its needed.  I would really appreciate any suggestions at all.  Thanks in advance.



f3=function(x) {
+ 
+  twodied=x[died==2,]; 
+  twosaved=x[saved==2,]; 
+  hit2=x[saved==2 & choice=="+",]; 
+  fa2=x [died==2 & choice=="+",]; d2=(qnorm(((nrow(hit2))/(nrow(twosaved))))-qnorm((nrow(fa2))/(nrow(twodied))));
+  hitrate=((nrow(hit2))/(nrow(twosaved))); FArate=((nrow(fa2))/(nrow(twodied)));
+  zhit2=qnorm(((nrow(hit2))/(nrow(twosaved))));zfa2=qnorm((nrow(fa2))/(nrow(twodied)));B2= -.5*(zhit2+zfa2);
+  tp2=mean(twosaved$resp); 
+  ta2=mean(twodied$resp); 
+  tpsd2=sd(twosaved$resp); 
+  tasd2=sd(twodied$resp); 
+  dmj2=((tp2-ta2)/((tasd2+tpsd2)/2));
+  
+ L=list(d2=d2,B2=B2,dmj2=dmj2,tpsd2=tpsd2,tasd2=tasd2,tp2=tp2,ta2=ta2,hitrate=hitrate,FArate=FArate) }


Jasper Jancik
910.352.7546


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: SS2full.txt
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061022/b9241ad2/attachment.txt 


More information about the R-help mailing list