Michael,

The function aggregate() is not going to work for your situation.  The 
function is applied to the individual columns of the subsetted data, not 
the subsetted data frame as a whole.  The help file reads: "Then, each of 
the variables (columns) in x is split into subsets of cases (rows) of 
identical combinations of the components of by, and FUN is applied to each 
such subset with further arguments in ... passed to it."

If you can rewrite your function so that it is a function with one 
argument, the data frame alone, then using the by() function should give 
you what you need.  Here is a simple example:

df <- data.frame(a=1:5, b=2:6, i=c(1, 1, 1, 2, 2))

junk <- function(df) {
        sum(df$a^2) + prod(df$b)
        }

data.frame(index=sort(unique(df$i)), results=as.vector(by(df[, c("a", 
"b")], df$i, junk)))

Hope this helps.

Jean


`·.,,  ><(((º>   `·.,,  ><(((º>   `·.,,  ><(((º>

Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409  USA



From:
"Michael Karol" <MKarol@syntapharma.com>
To:
<r-help@r-project.org>
Date:
08/02/2011 09:35 AM
Subject:
[R] Help with aggregate syntax for a multi-column function please.
Sent by:
r-help-bounces@r-project.org



Dear R-experts:

 

I am using a function called AUC whose arguments are data, time, id, and
dv.
 
data is the name of the dataframe, 
time is the independent variable column name, 
id is the subject id and 
dv is the dependent variable. 
 
The function computes area under the curve by trapezoidal rule, for each
subject id.
 
I would like to embed this in aggregate to further subset by each Cycle,
DoseDayNominal and Drug, but I can't seem to get the aggregate syntax
correct.  All the examples I can find use single column function such as
mean, whereas this AUC function requires four arguments.
 
Could someone kindly show me the syntax?
 
This is what I've tried so far:
 
AUC.DF<- aggregate(PKdata, list(PKdata$Cycle, PKdata$DoseDayNominal,
PKdata$Drug), 
                   function(x,tm,pt,conc) {AUC(x)},
tm="TimeBestEstimate", pt="Pt", conc="ConcentrationBQLzero" )
 
AUC.DF<- aggregate(PKdata, list(PKdata$Cycle, PKdata$DoseDayNominal,
PKdata$Drug), 
                   function(x) {AUC(x,"TimeBestEstimate", "Pt",
"ConcentrationBQLzero" )} )
 
AUC syntax is:
args(AUC)
function (data, time = "TIME", id = "ID", dv = "DV") 
 
 
thanks

 

Regards, 

Michael

 


                 [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



	[[alternative HTML version deleted]]

