liori
liori

Reputation: 42387

R, programmatically give name of column

I've got a function to do ANOVA for a specific column (this code is simplified, my code does some other related things to that column too, and I do this set of calculations for different columns, so it deserves a function). alz is my dataframe.

analysis <- function(column) {
 print(anova(lm(alz[[column]] ~ alz$Category)))
}

I call it e.g.:

analysis("VariableX")

And then in the output I get:

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 

How to make the output show the column name instead of alz[[column]]?

Upvotes: 1

Views: 401

Answers (2)

IRTFM
IRTFM

Reputation: 263481

analysis <- function(column) { 
   afit <- anova(lm( alz[[column]] ~ alz$Category))
   attr(afit, "heading") <- sub("\\: .+$", paste(": ", column) , attr( afit, "heading") )
   print(afit)
}

The anova object carries its "Response:" value in an attribute named "heading". You would be better advised to use the 'data' argument to lm in the manner @kohske illustrated.

Upvotes: 1

kohske
kohske

Reputation: 66902

Here is an example:

> 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 

Upvotes: 9

Related Questions