如何在R中获得公式的长度?

发布于 2025-01-28 05:00:32 字数 445 浏览 3 评论 0原文

我正在尝试获取以下公式的长度:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平安喜乐 2025-02-04 05:00:32

我们可以使用all.vars获取公式中的变量,然后应用length

length(all.vars(myformula))
[1] 5

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

length(all.vars(myformula))
[1] 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文