私は SO でかなりの量の読書をして、一般的にformula objects
as 文字列の操作を避けるべきであることを学びましたが、安全な方法でこれを行う方法をまだ見つけていません:
tf <- function(formula = NULL, data = NULL, groups = NULL, ...) {
# Arguments are unquoted and in the typical form for lm etc
# Do some plotting with lattice using formula & groups (works, not shown)
# Append 'groups' to 'formula':
# Change y ~ x as passed in argument 'formula' to
# y ~ x * gr where gr is the argument 'groups' with
# scoping so it will be understood by aov
new_formula <- y ~ x * gr
# Now do some anova (could do if formula were right)
model <- aov(formula = new_formula, data = data)
# And print the aov table on the plot (can do)
print(summary(model)) # this will do for testing
}
おそらく、私が最も近かったのは使用することでしたが、それは RHSreformulate
のみを提供し、 . 次のような関数を使用したい:+
*
p <- tf(carat ~ color, groups = clarity, data = diamonds)
カラット〜カラー*クラリティのaov結果があります。前もって感謝します。
解決
これは、何が起こっているかを示す @Aaron のコメントに基づく作業バージョンです。
tf <- function(formula = NULL, data = NULL, groups = NULL, ...) {
print(deparse(substitute(groups)))
f <- paste(".~.*", deparse(substitute(groups)))
new_formula <- update.formula(formula, f)
print(new_formula)
model <- aov(formula = new_formula, data = data)
print(summary(model))
}