0

I use reformulate to construct formulas involving a set of indicators for Reddit usernames. Unfortunately, reformulate seems to concatenate a string out of the passed term names and parse it, which leads to some weird problems for terms that are valid usernames, but not valid identifiers:

> reformulate(c("123a", "x"), "y")
Error in str2lang(termtext) : <text>:1:4: unexpected symbol
1: 123a
       ^

Even worse, something like this can happen:

> reformulate(c("x", "a-b-1", "z"), "y")
y ~ x + a - b - 1 + z

How can I remedy this using some kind of "escape mechanism", and without resorting to string processing?

phipsgabler
  • 20,535
  • 4
  • 40
  • 60

1 Answers1

1

Quote every term name using backticks, to specify each username as a separate non-syntactic name:

> paste0("`", c("123a", "a-b-1", "z"), "`")
[1] "`123a`"  "`a-b-1`" "`z`" 
> reformulate(paste0("`", c("123a", "a-b-1", "z"), "`"), "y")
y ~ `123a` + `a-b-1` + z

The backticks don't become part of the term names. As you can see with z, syntactic names stay unchanged.

phipsgabler
  • 20,535
  • 4
  • 40
  • 60