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.