[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) {
		dt <- data.frame(x=x, group=y)
	else {
		dt <- with(data, data.frame(x=x, group=y))


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?


More information about the R-help mailing list