[R] getting the by() function to work properly
chao gai
chaogai at duineveld.demon.nl
Mon Oct 23 18:53:38 CEST 2006
Hi Jasper,
For me it seems that you are fooling around with attach() or something.
For one thing, it will not work like this,:
e.g. twodied=x[died==2,] is the first statement
died is not locally defined, hence it is gotten from somewhere else...
For the second thing, this will throw an error:
by(ss3, sub, f3)
sub is defind (unless attached from some data.frame), it is a statement.
Both these seem to suggest that somewhere there is a version of your dataframe
attached from which values are being read.
As a quick fix I made the following:
f3 <- function(x) {
attach(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))
detach(x)
L=list(d2=d2,B2=B2,dmj2=dmj2,tpsd2=tpsd2,tasd2=tasd2,tp2=tp2,ta2=ta2,hitrate=hitrate,FArate=FArate)
return(L)
}
f3(SS2)
by(SS2, SS2$sub, f3)
It did not throw an error for me.
As a note, could you not use table() or xtabs() for some of these
calculations? All this nrow() stuff does not look very efficient.
Kees
On Monday 23 October 2006 04:49, Jancik, Jasper F. wrote:
> 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(t
>wodied)));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,hitrat
>e=hitrate,FArate=FArate) }
>
>
> Jasper Jancik
> 910.352.7546
More information about the R-help
mailing list