[R] Specification of factors in tapply

rijn@swi.psy.uva.nl rijn at swi.psy.uva.nl
Wed Feb 21 16:29:44 CET 2001


After some fiddling around with the tapply command, I discovered that the
factors (the INDEX argument) given to tapply must be specified in
fastest-cycling first order. 

The following code shows how I discovered my error: (R version 1.2.1)

-o-o-o-o-o-

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}))

-o-o-o-o-

resulting in:

> x
   data subj cond first
1    -9    1    1 FALSE
2     0    1    1  TRUE
3     3    1    1 FALSE
4     1    1    1 FALSE
5    -9    1    2 FALSE
6     1    1    2  TRUE
7     0    1    2 FALSE
8    -9    2    1 FALSE
9     0    2    1 FALSE # <--
10    3    2    1  TRUE # <--
11    1    2    1 FALSE
12   -9    2    2 FALSE
13    1    2    2  TRUE
14    0    2    2 FALSE

I could not find any reference to this order in the tapply help file nor
in "An Introduction to R" (Version 1.2.1 (2001-01-15), PDF file p17), it
might prove useful to include some information about this. (Especially
since the help text for functions like order do include information how to
specify the factors: slowest-cycling first.)

Groet,
  Hedderik.

-- 
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