Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the length of a formula in R?

I am trying to get the length of the following formula:

myformula <- ~ (1 | Variable1) + (1|Sex) + Age + Cells + Glucose

But for some reason, R doesn’t recognize the real number of elements (which is 5)

str(myformula)
Class 'formula'  language ~(1 | Variable1) + (1 | Sex) + Age + Neutrophils + Monocytes
  ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 

length(myformula)
[1] 2

(Note: I have variables like this (1|Variable) because I am working with the Variance Partition package (and categorical variables must be written in that format).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anyone know how to get the real length of a formula in R?

Thanks very much in advance

Regards

>Solution :

We may use all.vars to get the variables in the formula and then apply the length

length(all.vars(myformula))
[1] 5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading