假设我有一个 Formula
列表:
library(Formula)
models <- list(y ~ x1, y ~ x2, y ~ x3)
models <- lapply(models, as.Formula)
假设我想将它们更新为y ~ 1 | x1 | 1
、y ~ 1 | x2 | 1
和y ~ 1 | x3 | 1
(例如,与mnlogit
一起使用)。如果我使用lapply
和update
,就可以在结尾添加| 1
:
lapply(models, update, . ~ . | 1) # Works as expected
# [[1]]
# y ~ x1 | 1
#
# [[2]]
# y ~ x2 | 1
#
# [[3]]
# y ~ x3 | 1
但是我不能在开头添加1 |
:
lapply(models, update, . ~ 1 | . | 1) # Fails -- why is the dot not replaced?
# [[1]]
# y ~ 1 | . | 1
#
# [[2]]
# y ~ 1 | . | 1
#
# [[3]]
# y ~ 1 | . | 1
有人知道为什么会出现这种情况,或者如何修复它(当然,除了逐个修复每个模型之外)吗?