[R] Passing variable names in quotes to a function

Brant Inman brant.inman at me.com
Wed Dec 2 06:11:04 CET 2015


I am trying to build a function that can accept variables for a regression.  It would work something like this:

---
# Y = my response variable (e.g. income)
# X = my key predictor variable (e.g. education)
# subY = a subsetting variable for Y (e.g. race)
# subY.val = the value of the subsetting value that I want (e.g. ‘black’)

foo <- function(Y, X, subY, subY.val, dataset){
  
  if(is.na(subY) == F) {
     Y <- paste(Y, ‘[‘, subY, ‘==‘, subY.val, ‘]’)
  }
  FORMULA <- paste(Y ~ X)
  fit <- some.regression.tool(FORMULA, data=dataset)

  return(some.data.after.processing)
}
---

If I call this function with, foo(income, education, race, “black”, my.dataset), I do not get the result that I need because the FORMULA is "income[race==black] ~ education” when what I need is “income[race==‘black’] ~ education”.  How do I get the quotes to stay on ‘black’?  Or, is there a better way?

Help appreciated.

--
Brant
	[[alternative HTML version deleted]]



More information about the R-help mailing list