s-news
[Top] [All Lists]

[S] tapply: problem, solution, question

To: s-news@wubios.wustl.edu
Subject: [S] tapply: problem, solution, question
From: Matthew Wiener <mcw@ln.nimh.nih.gov>
Date: Wed, 29 Apr 1998 10:10:41 -0400 (EDT)
Sender: owner-s-news@wubios.wustl.edu
Hello, all.

I have been using tapply for a while for univariate statistics -- things
like   tapply(x$response, x$stimulus, mean).  

So when I wanted to do some multivariate statistics, like correlation, I
tried  tapply(x[,c("response1","response2")],x$stimulus,cor)
(also variations with cor replaced by function(x) cor(x) and so on).

I consistently got the error message:
Error in tapply(cbind(kofs009$sp, kofs009$lat), ko..: Data and all indices
must have same length

After some digging in the documentation I was able to work around the
problem by using "by", which creates a list of the answers, and sapply,
which lets me turn the list into a vector: 

>> sapply(by(x,x$stimulus,function(x) cor(x$response1,x$response2)),
>>                                      function(x)  x,simplify=T) 

which gives me what I want.

My question is:  is this the best way?  
Also, why can't tapply handle this?  (Especially given that the
documentation lists "by" as "a convenient, object-oriented version of
tapply".)

Thanks,

Matthew Wiener
NIMH 


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>