如何在R中获得公式的长度?
我正在尝试获取以下公式的长度:
myformula <- ~ (1 | Variable1) + (1|Sex) + Age + Cells + Glucose
但是由于某种原因,r
无法识别元素的实际数量(5)
str(myformula)
Class 'formula' language ~(1 | Variable1) + (1 | Sex) + Age + Neutrophils + Monocytes
..- attr(*, ".Environment")=<environment: R_GlobalEnv>
length(myformula)
[1] 2
(注意:我有这样的变量(1 | )因为我正在使用方差分区软件包(并且必须以该格式编写分类变量)。
变量
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).
Does anyone know how to get the real length of a formula in R
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
all.vars
获取公式中的变量,然后应用length
We may use
all.vars
to get the variables in the formula and then apply thelength