0

I have multiple Red files that I am joining with bookdown. This has gone OK until I got the following error message label: error_chunk

Quitting from lines 2941-2947 (bookdown-demo.Rmd) Error in model.frame.default(formula = glu ~ age, drop.unused.levels = TRUE) : variable lengths differ (found for 'age') Calls: ... lm -> eval -> eval -> -> model.frame.default Execution halted

Exited with status 1.

error_chunk is the following code:-

```{r error_chunk}
sample1 <- data.frame(glu=c(120,130,140,150), age=c(55,60,65,70))
attach(sample1)
#For linear regression analysis, the function lm (linear model) is used
fit<-lm (glu~age)
print(fit)
```

This is straightforward and obviously runs on its own and produces its own Rmd document.

What is occurring in the bookdown environment?

sm925
  • 2,648
  • 1
  • 16
  • 28

1 Answers1

0

The code works in bookdown if I use the variable names sample1$glu & sample1$age. Don't understand why attach() doesn't seem to work.

  • 1
    a better approach would be to use the `data` argument of `lm` . So `lm (glu ~ age, data=sample1)` . (ps dont use `attach` - it will lead to problems) – user20650 Jan 05 '18 at 16:41