I am having trouble with variable scoping inside/outside functions. I want to use get() to grab a named and quoted variable as an input to a function, however the function can't find the variable when it is entered into the function call, only when it is named in the main environment.
I obviously am not so clear on variable scoping, and ?get really doesn't clear up my confusion. I am sure that there are better, more appropriate ways to write a function... Please enlighten me, Pat # Example code dat <- expand.grid(a = factor(c("a", "b")), b=1:10) # Function that requires get() ex <- function(data, response){ library(plyr) output <- ddply(data, .(a), summarize, res = sum(get(response)) ) return(output) } out <- ex(data = dat, response = "b") # Error in get(response) : object 'response' not found # However if I name reponse outside of the function, it is found by the function response = "b" out <- ex(data = dat, response = "b") out #> out # a res #1 a 55 #2 b 55 -- Patrick Schmitz Graduate Student Plant Biology 1206 West Gregory Drive RM 1500 [[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.