R中的Anova问题[重复]

lskq00tm  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(241)
    • 此问题在此处已有答案**:

Is there a way to loop through column names (not numbers) in r for linear models?(5个答案)
using lm function in R with a variable name in a loop(2个答案)
5天前关闭。
This is a picture of my column names in a value and then the anova that I have set up for it that works
我想做一个能够在一个循环中切换出anova中的每一个列名的程序,这样我就不必重写代码,只需要改变列名。
尝试设置for循环,但无法使其工作。

for (i in col_values) {
 one.way <- aov(i ~ Treatment, treatment_data)
}

我想我会问这里。不是特别需要坚持for循环,所以开放的任何和所有的建议。感谢任何建议,你可能有!

uinbv5nw

uinbv5nw1#

尝试将其 Package 在公式调用中:

for (i in col_values) {
 one.way <- aov(as.formula(paste(i, " ~ Treatment")), treatment_data)
}
rsl1atfo

rsl1atfo2#

library(tidyverse)

y <- names(mtcars)[1:3] %>% 
  set_names()

map(y, ~aov(reformulate(.x, "qsec"), data = mtcars))
#> $mpg
#> Call:
#>    aov(formula = reformulate(.x, "qsec"), data = mtcars)
#> 
#> Terms:
#>                      mpg Residuals
#> Sum of Squares  17.35226  81.63589
#> Deg. of Freedom        1        30
#> 
#> Residual standard error: 1.649605
#> Estimated effects may be unbalanced
#> 
#> $cyl
#> Call:
#>    aov(formula = reformulate(.x, "qsec"), data = mtcars)
#> 
#> Terms:
#>                      cyl Residuals
#> Sum of Squares  34.60301  64.38514
#> Deg. of Freedom        1        30
#> 
#> Residual standard error: 1.464982
#> Estimated effects may be unbalanced
#> 
#> $disp
#> Call:
#>    aov(formula = reformulate(.x, "qsec"), data = mtcars)
#> 
#> Terms:
#>                     disp Residuals
#> Sum of Squares  18.61906  80.36909
#> Deg. of Freedom        1        30
#> 
#> Residual standard error: 1.636756
#> Estimated effects may be unbalanced

创建于2023年2月27日,使用reprex v2.0.2

相关问题