s-news
[Top] [All Lists]

Help with function

To: <s-news@lists.biostat.wustl.edu>
Subject: Help with function
From: "Roberts, J. Kyle" <jkrobert@bcm.tmc.edu>
Date: Tue, 9 Jan 2007 09:23:57 -0600
In-reply-to: <223239.17207.qm@web90312.mail.mud.yahoo.com>
Thread-index: AccaCS6yzTgkxCj2QmqscmciqF9rbgZ95ReQ
Thread-topic: Help with function
Dear All,
 
I need to know how to create different pieces of a function that I have created.  For example, in aov, one could type aov$contrasts to obtain the contrasts.  I want to use this for another application, but have simplified it here.
 
Suppose that I have the following:
new.data<-matrix(c(1:10, rep(1:5, each=2)), ncol=2)
 
average<-function(x,y){
     out<-lapply(split(x,y), mean)
     mean(unlist(out))
}
 
new.out<-average(new.data[,1], new.data[,2])
new.out
 
This is just a simple function that produces the mean from a group of means (in this case they all have the same no. of observations).  Suppose, however, that after I run the function, I would like to actually see the means of each of the groups based on the factor variable (new.data[,2]).  In other words, I want to see the output from the command lapply(split(x,y), mean) .
 
How do I need to change my function so that I can obtain this by just typing something like:
new$out
 
Thanks,
Kyle
 
***************************************
J. Kyle Roberts, Ph.D.
Baylor College of Medicine
Center for Educational Outreach
One Baylor Plaza, MS:  BCM411
Houston, TX   77030-3411
713-798-6672 - 713-798-8201 Fax
jkrobert@bcm.edu
***************************************
<Prev in Thread] Current Thread [Next in Thread>
  • Help with function, Roberts, J. Kyle <=