[R] Function with 'data' parameter

Greg chaoborid at gmail.com
Sun Feb 22 05:25:18 CET 2009


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.




More information about the R-help mailing list