[R] Error in model.frame

Martyn Plummer plummer at iarc.fr
Wed Dec 10 11:02:27 CET 2003


On Wed, 2003-12-10 at 10:34, Göran Broström wrote:
> I recently ran into the following:
> 
> > x <- c(1,2,4,3)
> > lm(x ~ 1:length(x))
> Error in model.frame(formula, rownames, variables, varnames, extras, extranames,  : 
>         variable lengths differ
> 
> but
> 
> > lm(x ~ c(1:length(x)))
> 
> Call:
> lm(formula = x ~ c(1:length(x)))
> 
> Coefficients:
>    (Intercept)  c(1:length(x))  
>            0.5             0.8  
> 
> and
> 
> > identical(1:4, c(1:4))
> [1] TRUE
> 
> Should I report this as a bug?

No. In a formula ":" is interpreted differently.
It is used to represent interactions. See help(formula).
It is probably good practice to generate variables outside of
the formula and then refer to them by name.

Martyn




More information about the R-help mailing list