Dear all, How can I replace text in objects that are of class "formula"? y="a * x + b" class(y)="formula" grep("x",y) y[1] Suppose I would like to replace the "x" by "w" in the formula object "y". How can this be done? Somehow, the methods that can be used in character objects do not work 1:1 in formula objects... Many thanks and best wishes Christoph -- Dr. rer.nat. Christoph Scherber University of Goettingen DNPW, Agroecology Waldweg 26 D-37073 Goettingen Germany phone +49 (0)551 39 8807 fax +49 (0)551 39 8806 Homepage http://www.gwdg.de/~cscherb1
On Thu, 6 Nov 2008, Christoph Scherber wrote:> Dear all, > > How can I replace text in objects that are of class "formula"? > > y="a * x + b" > class(y)="formula" > grep("x",y) > y[1]What exactly are you trying to accomplish?? And why did you assign 'formula' as the class of a character string? 'y' is not a valid formula object:> lm(y)Error in terms.formula(formula, data = data) : argument is not a valid model ==== Perhaps, you need to review ?formula and 11 Statistical models in R from Introduction to R. Oh, yes. There is the matter of reviewing the _posting guide_ before posting, too. HTH, Chuck> > Suppose I would like to replace the "x" by "w" in the formula object "y". > > How can this be done? Somehow, the methods that can be used in character > objects do not work 1:1 in formula objects... > > Many thanks and best wishes > Christoph > > > > -- > Dr. rer.nat. Christoph Scherber > University of Goettingen > DNPW, Agroecology > Waldweg 26 > D-37073 Goettingen > Germany > > phone +49 (0)551 39 8807 > fax +49 (0)551 39 8806 > > Homepage http://www.gwdg.de/~cscherb1 > > ______________________________________________ > R-help at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Hi, Firstly, I'd recommend using '<-' for assignment, rather than '='; it saves confusion Secondly, I don't think you want 'a*x+b' as a formula, I think you want an expression. Thirdly, your 'y' has only one term, a 9 character constant = "a * x + b" Consider instead, y <- expression(a*x+b) a <- 2 b <- 3 x <- 1:10 y eval(y) Now, how to replace 'x' by 'w'? I'm not an expert, but this is the kind of thing I need to do, so I'd welcome criticism of my approach. I would view the expression as a list: as.list(y) as.list(y[[1]]) So y is an expression containing a sub-expression; that is y is '(a*x) + b' You want to access the sub-expression 'a*x' y[[1]][[2]] as.list(y[[1]][[2]]) Now you want to replace the third item in that sub-expression with the name (not the character) w y[[1]][[2]][[3]] <- as.name("w") w <- 11:20 y eval(y) hth Keith J P.S. Perhaps you really do want a formula; y ~ a*x+b ?? In that case I'd still probably manipulate it as a list. ------------------------------------------- "Christoph Scherber" <Christoph.Scherber at agr.uni-goettingen.de> wrote in message news:4913125C.2060500 at agr.uni-goettingen.de...> Dear all, > > How can I replace text in objects that are of class "formula"? > > y="a * x + b" > class(y)="formula" > grep("x",y) > y[1] > > Suppose I would like to replace the "x" by "w" in the formula object "y". > > How can this be done? Somehow, the methods that can be used in character > objects do not work 1:1 in formula objects... > > Many thanks and best wishes > Christoph > > > > -- > Dr. rer.nat. Christoph Scherber > University of Goettingen > DNPW, Agroecology > Waldweg 26 > D-37073 Goettingen > Germany > > phone +49 (0)551 39 8807 > fax +49 (0)551 39 8806 > > Homepage http://www.gwdg.de/~cscherb1