[R] Specification of factors in tapply
Hedderik van Rijn
rijn at swi.psy.uva.nl
Wed Feb 21 18:14:56 CET 2001
First of all, thanks for the helpful reply (also to Peter Dalgaard).
Ripley:
> I think you are just discovering that if you collapse an array to a
> vector, you get the results in Fortran order.
Dalgaard:
> tapply() gives a *table*, in this case each element is a vector of
> logicals. Tables are stored with first index varying the fastest. So
> where's the surprise?
Obviously, I overlooked the exact representation of the result of tapply.
A nice example of conformation bias as the unlist command seemed the right
function to use but clouded my the actual return structure. :-)
If I understand correctly, my code depends on the "accidental" order of
table representations. Browsing through of R scripts I wrote, it seems that
I've been very lucky not to stumble upon this earlier as I use this
"feature" quite often.
So, two questions pop up.
First, can I use this "feature" savely, or is should it be considered
"undocumented" and instable? (I do not like the idea of possible different
results if I ever happen to run the scripts using later versions of R.)
Second, what are the functions/methods I could use to get these results in a
more R-ish way? I quite often want to perform some function per subset of a
dataframe and include the results back into the frame. As in the example I
sent, finding the first value equal to or larger than 0 per
subject/condition combination.
Thanks a lot for all the help sofar,
Hedderik.
(Just to be sure, I include the example again:)
x <- as.data.frame(list(data=c(-9,0,3,1,-9,1,0,-9,0,3,1,-9,1,0),
subj=c(rep(1,7),rep(2,7)),
cond=rep(c(rep(1,4),rep(2,3)),2)))
x$first <- unlist(tapply(x$data,list(x$subj,x$cond),
function(x) {
retval<-rep(F,length(x));
if (length(x[x>=0])>0) {
retval[min(which(x>=0))]<-T;
}
print(cbind(x,retval)); # Print some debug info
retval}))
--
http://swipc30.swi.psy.uva.nl/~rijn
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list