0

I am trying to use the pymer4 package in python for Multi-Level Models, and I have to pass an "R-style formula specifying the model". I understand that it looks like:

target ~ varA + varB

However, I have many variables and some of those have numbers at the end of the names, parentheses, and slashes, like:

var1, (var)2, var1/var2  

When I create a string like in the first example, I get the following error, indicating a variable with a number as the suffix.

RRuntimeError: Error in str2lang(x) : <text>:1:70: unexpected numeric constant  

I know I could change the names, but it is tedious as I have many variables, as well as having a lot of other code that uses the existing names. I have never coded in R so I haven't got a clue how to solve this problem. There must be a way to just specify that the number and other characters are part of the string, no?

6
  • 2
    wrap those "problem@t1c / names" in quotations or backticks. Commented Nov 11 at 21:44
  • Possible duplicate of stackoverflow.com/questions/73514811/… Commented Nov 11 at 21:48
  • 1
    I was thinking backticks. I don't use pymer4, but I can produce that error with as.formula("var1 ~ (var)2 + var1/var2") and the error does not occur with as.formula("`var1` ~ `(var)2` + `var1/var2`"). While I don't assume that you're calling as.formula or str2lang directly, the backticks should cascade into where the error is being triggered. Commented Nov 12 at 1:29
  • This looks like an xy problem. Your "variable names" don't look like something you should be creating in R or python. Commented Nov 12 at 6:04
  • The brackticks solved the problem! @Roland - I wouldn't usually name variables like this, but these are column names in a DataFrame, so I felt I had a little more freedom in naming them. Commented Nov 12 at 8:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.