Search code examples
rfunctionanova

Receiving an error when using aov() inside a function


I am trying to use the aov() function inside a function but R keeps giving me the same error.

Code:

dat$X1 = rep(c("a", "b"), 2)
dat$X2 = c(1,2,3,4)

f = function (x){
  aov(x ~ X1 , data = dat)
}
f('X2')

This gives me the following error:

Error in model.frame.default(formula = x ~ X1, data = dat, drop.unused.levels = TRUE) : 
variable lengths differ (found for 'X1')

The aov() works when I try to replace 'x' with the actual name of the variable (X2) so it doesn't make sense that the variable lengths would differ.

I have looked for this error everywhere but so far I haven't had luck finding the same error anywhere else.

I'm pretty sure that I am overlooking something very obvious but I've been stuck with this for a while.

Looking forward to reading your advise. Thanks.


Solution

  • If you want to use function, and aov inside it, you may try

    dat <- data.frame(X1 = rep(c("a", "b"), 2), X2 = c(1,2,3,4))
    f = function (x){
      ff <-as.formula(paste0(x, "~ X1")) 
      aov(ff , data = dat)
    }
    f('X2')
    
    Call:
       aov(formula = ff, data = dat)
    
    Terms:
                    X1 Residuals
    Sum of Squares   1         4
    Deg. of Freedom  1         2
    
    Residual standard error: 1.414214
    Estimated effects may be unbalanced