the easiest way is probably to define the function in a global environment:
myfunction <- function (data, response, pred, covar, scale) {
data$covarcl <- round(data[,covar]/scale)*scale
data.tmp <- data[order(data$covarcl), ]
freq <<- function(data.tmp) sum(data.tmp[,response]==1)
mpred <<- function(data.tmp) mean(data.tmp[,pred])
mresponse <<- function(data.tmp)mean(data.tmp[,response])
data.tmp2 <-ddply(data.tmp, .(covarcl), c("freq","mpred","mresponse"))
rm("freq", "mpred", "mresponse", inherits = TRUE)
return(data.tmp2)
}
myfunction(df, "observed", "fitted", "dhp", 2)
--
Kohske Takahashi <takahash...@gmail.com>
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
> --
> You received this message because you are subscribed to the Google Groups "manipulatr" group.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.
>
>
Yes, see here https://github.com/hadley/plyr/issues#issue/3
This is turning out to be really tricky to fix in general - it's a
violation of R's usual static scoping rules, and it's very hard to get
right. lapply does everything at the C level and works by manually
creating calls and setting their parent to the correct environment.
> Secondly, the correct, principled way to do this is to pass the functions by
> value, not to pass their names. But though ddply accepts individual
> functions either by name or by value, lists of functions must be specified
> by names, not by value. Ugly. See below for example.
This was an easy fix and will be present in the next version of plyr.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/