I'm trying to write a simple function with a data parameter.

tfun <- function(x, y, data = NULL) {
        if(missing(data))
                dt <- data.frame(x=x, group=y)
        else {
                dt <- with(data, data.frame(x=x, group=y))
        }

        return(dt)
}

If I pass variables "weight" and "grp" from a data.frame, d, like
this: tfun(d$weight, d$grp), the function works.  However, if I try to
do the same thing by supplying d, like this: tfun(weight, grp,
data=d), I receive the following error:

Error in data.frame(x = x, group = y) : object "weight" not found

Can someone please tell me what I'm doing wrong?

Greg.

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

Reply via email to