R,以编程方式给出列的名称

lio*_*ori 1 r

我有一个函数来为特定列做一个ANOVA(这个代码被简化了,我的代码也做了一些其他相关的事情,我为不同的列做了这组计算,所以它值得一个函数).alz是我的数据帧.

analysis <- function(column) {
 print(anova(lm(alz[[column]] ~ alz$Category)))
}
Run Code Online (Sandbox Code Playgroud)

我称之为:

analysis("VariableX")
Run Code Online (Sandbox Code Playgroud)

然后在输出中得到:

Analysis of Variance Table

Response: alz[[column]]
              Df Sum Sq Mean Sq F value    Pr(>F)    
alz$Category   2  4.894 2.44684  9.3029 0.0001634 ***
Residuals    136 35.771 0.26302                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
Run Code Online (Sandbox Code Playgroud)

如何使输出显示列名而不是alz[[column]]

koh*_*ske 9

这是一个例子:

> f <- function(n) {
+   fml <- as.formula(paste(n, "~cyl"))
+   print(anova(lm(fml, data = mtcars)))
+ }
> 
> f("mpg")
Analysis of Variance Table

Response: mpg
          Df Sum Sq Mean Sq F value    Pr(>F)    
cyl        1 817.71  817.71  79.561 6.113e-10 ***
Residuals 30 308.33   10.28                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
Run Code Online (Sandbox Code Playgroud)