Jun Shen
2013-Dec-16 15:11 UTC
[R] How to plug in characters as object names in a formula (recast from package reshape2 specifically)
Hi everyone, This may be very simple but I couldn't figure it out. I have this function rsm.lm<-function(data,xvar='xCmin',yvar='yCmin') { #some calculation...... recast(df, xvar~yvar, id.var=1:2, measure.var=3) #some other operations } df is a dataframe defined internally within the function with two id variables (xCmin and yCmin) and a measure variable. recast doesn't work and keep saying "Casting formula contains variables not found in molten data: xvar, yvar......" I tried as.name(xvar)~as.name(yvar) get(xvar)~get(yvar) cat(xvar)~cat(yvar) eval(cat(xvar))~eval(cat(yval)) But none of these works. It only works when I explicitly specify the formula as recast(df, xCmin~yCmin, id.var=1:2, measure.var=3) Is there a workaround this issue? Thanks. Jun [[alternative HTML version deleted]]
Adams, Jean
2013-Dec-17 16:57 UTC
[R] How to plug in characters as object names in a formula (recast from package reshape2 specifically)
Try this (untested code): recast(df, formula(paste(xvar, "~", yvar)), id.var=1:2, measure.var=3) Jean On Mon, Dec 16, 2013 at 9:11 AM, Jun Shen <jun.shen.ut@gmail.com> wrote:> Hi everyone, > > This may be very simple but I couldn't figure it out. I have this function > > rsm.lm<-function(data,xvar='xCmin',yvar='yCmin') { > > #some calculation...... > > recast(df, xvar~yvar, id.var=1:2, measure.var=3) > #some other operations > } > > df is a dataframe defined internally within the function with two id > variables (xCmin and yCmin) and a measure variable. recast doesn't work and > keep saying "Casting formula contains variables not found in molten data: > xvar, yvar......" > > I tried > > as.name(xvar)~as.name(yvar) > get(xvar)~get(yvar) > cat(xvar)~cat(yvar) > eval(cat(xvar))~eval(cat(yval)) > > But none of these works. It only works when I explicitly specify the > formula as > > recast(df, xCmin~yCmin, id.var=1:2, measure.var=3) > > Is there a workaround this issue? Thanks. > > Jun > > [[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. >[[alternative HTML version deleted]]