On 2025-03-29 10:30 p.m., Bert Gunter wrote:
As always, I would like to thank all who responded for their insights and
suggestions. I have learned from them.

Thus far, my own aesthetic preference -- and therefore not to be considered
in any sense as a "best" approach -- is to use Duncan's suggestion to
produce the call directly with call() rather than substitute in my simple
for() loop; i.e.

somenames <- c("Heigh", "Ho", "Silver", "Away")
nms <- lapply(somenames, as.name)
frm<- nms[[1]]
for(x in nms[-1]) frm <<- call("+", frm, x)
frm <-bquote(~ (.(frm))^2, list(frm =frm))

## which yields

frm
~(Heigh + Ho + Silver + Away)^2

A couple of comments:

1. Using <<- in the for loop is a bad idea. It works fine at the top level, but if you put that code in a function, it would try to make the assignment outside the scope of the function. Standard <- assignment is better here.

2. You can use Reduce() to replace the for loop. Some people would prefer that:

  Reduce(\(x,y) call("+", x, y), nms)

produces the same result as the loop.

Duncan Murdoch


I obviously still did not make make my requirements clear, since after
converting the character string names into language onjects via as.name(),
the above (and Duncan's response and my original query) do not make any use
of string parsing/conversion. That is what I meant. As respondents pointed
out, there are some base R functions that put the string manipulations
behind a curtain so that one need not directly worry about them, but I
didn't want to go there either. Again, my apologies for my imprecision.

Finally, I still welcome any other approaches along these lines and
especially would appreciate anyone pointing out any fatal flaws or even
modest infelicities in the above approach.

Cheers,
Bert

"An educated person is one who can entertain new ideas, entertain others,
and entertain herself."



On Sat, Mar 29, 2025 at 6:32 PM Ebert,Timothy Aaron <teb...@ufl.edu> wrote:

I am confused. Richard's answer that Bert did not like did not use parse
explicitly. Richard pasted together a string that a function like lm() will
have to parse to run the analysis. However, the answers so far do not use
parse(). In the reply to Richard, Bert indicated we cannot use strings.
Even if I pass a vector where R can assume that the first variable is the
dependent variable and all others are independent variables, I see no way
of processing that within the current modeling system without generating a
formula (a string) that gets parsed.

-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Richard M.
Heiberger
Sent: Saturday, March 29, 2025 8:47 PM
To: Bert Gunter <bgunter.4...@gmail.com>
Cc: R-help <R-help@r-project.org>
Subject: Re: [R] [External] Creating model formulas programmatically

[External Email]

my take of the assignment was to avoid 'parse' specifically.
we start with a character vector, so avoiding characters is not possible.
i was dealing with the fortune "if parse is the answer, you have the wrong
question"

Sent from my iPhone

On Mar 29, 2025, at 15:39, Bert Gunter <bgunter.4...@gmail.com> wrote:


Thanks, Rich.

I thought of that, too, but it violates the spirit of my restraints (to
avoid character strings), which I unfortunately did not clearly articulate.
So my apologies for that failure. My concern is that with more complex
model formula, using as.formula, etc. to parse/convert character strings
can get a bit hairy. But in most cases, as here maybe, it may be perfectly
fine. So think of my post as mostly my attempt to learn some new tricks
rather than to solve a useful problem. I hope this is not unfair to the
list.

Cheers,
Bert





On Sat, Mar 29, 2025 at 3:1 PM Richard M. Heiberger <r...@temple.edu
<mailto:r...@temple.edu>> wrote:
somenames <- c("Heigh", "Ho", "Silver", "Away")
as.formula(paste("~(",paste(somenames, collapse="+"),")^2"))
~(Heigh + Ho + Silver + Away)^2


On Mar 29, 2025, at 14:30, Bert Gunter <bgunter.4...@gmail.com<mailto:
bgunter.4...@gmail.com>> wrote:

somenames <- c("Heigh", "Ho", "Silver", "Away")



         [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to