I want to pass a predefined string ww ("fa*fb+fc") to function lme so that I can run > lme(y ~ fa*fb+fc, random = ~1|subj, model) I've tried something like > lme(y ~ paste(ww), random = ~1|subj, model) and > lme(y ~ sprintf(ww), random = ~1|subj, model) but both give me the following error: Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : variable lengths differ (found for 'ww') There must be a simple way to do this. Any help? Thanks, Gang
Vladimir Eremeev
2007-Sep-19 18:00 UTC
[R] How to pass a string as an argument to a function?
lme(as.formula(paste("y~",ww)),random=~1|subj,model) Gang Chen-3 wrote:> > I want to pass a predefined string ww ("fa*fb+fc") to function lme so > that I can run > > > lme(y ~ fa*fb+fc, random = ~1|subj, model) > > There must be a simple way to do this. Any help? > > Thanks, > Gang >-- View this message in context: http://www.nabble.com/How-to-pass-a-string-as-an-argument-to-a-function--tf4482519.html#a12782865 Sent from the R help mailing list archive at Nabble.com.
Yes, as.formula is the magic tool! Thanks a lot... Gang On Sep 19, 2007, at 2:00 PM, Vladimir Eremeev wrote:> > lme(as.formula(paste("y~",ww)),random=~1|subj,model) > > > Gang Chen-3 wrote: >> >> I want to pass a predefined string ww ("fa*fb+fc") to function lme so >> that I can run >> >>> lme(y ~ fa*fb+fc, random = ~1|subj, model) >> >> There must be a simple way to do this. Any help? >> >> Thanks, >> Gang