Please try to construct a reproducible example as I suggested. First, to use the function lm
, you must specify a formula. The function abline
adds a line to an existing plot. Is this what you want? Finally, the last line of your code is missing a closing parentheses which is causing the error you are seeing.
I've made some code that works but I'm really not sure it is what you want. What problem are you trying to solve?
library(tidyverse)
nrow <- 6
y <- tibble(Jan=rnorm(nrow))
x <- data.frame(matrix(rnorm(5*nrow), nrow=nrow)) %>% as_tibble()
y
#> # A tibble: 6 x 1
#> Jan
#> <dbl>
#> 1 -0.394
#> 2 -2.27
#> 3 0.00957
#> 4 1.15
#> 5 0.642
#> 6 -0.928
x
#> # A tibble: 6 x 5
#> X1 X2 X3 X4 X5
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -1.88 0.775 0.249 2.05 1.70
#> 2 0.512 1.14 0.233 -1.32 -0.513
#> 3 -0.620 0.462 -1.24 0.436 0.485
#> 4 0.491 1.22 -0.113 0.213 -0.166
#> 5 -1.00 -0.913 0.252 0.101 0.546
#> 6 0.201 -0.221 -0.533 0.948 -0.318
ab <- vector("list", length(x))
for (i in 1:length(x)){
dat <- data.frame(y=y$Jan, x=x[[i]])
ab[[i]] <- lm(y~x, data=dat)
}
ab
#> [[1]]
#>
#> Call:
#> lm(formula = y ~ x, data = dat)
#>
#> Coefficients:
#> (Intercept) x
#> -0.4128 -0.3009
#>
#>
#> [[2]]
#>
#> Call:
#> lm(formula = y ~ x, data = dat)
#>
#> Coefficients:
#> (Intercept) x
#> -0.1642 -0.3263
#>
#>
#> [[3]]
#>
#> Call:
#> lm(formula = y ~ x, data = dat)
#>
#> Coefficients:
#> (Intercept) x
#> -0.3467 -0.2533
#>
#>
#> [[4]]
#>
#> Call:
#> lm(formula = y ~ x, data = dat)
#>
#> Coefficients:
#> (Intercept) x
#> -0.453 0.383
#>
#>
#> [[5]]
#>
#> Call:
#> lm(formula = y ~ x, data = dat)
#>
#> Coefficients:
#> (Intercept) x
#> -0.4260 0.4434
Created on 2020-03-30 by the reprex package (v0.3.0)