Here is a sketch of how you can do that. I did not build a loop but you could change to code where is says

```
Results[names(vec1), 1] <- vec1
```

to

```
Results[names(vec1), i] <- vec1
```

and maybe make a few more adjustments.

By the way, you should not manually increment the value of i within the for loop. That is, do not have the line.

```
i <- i + 1
```

The *for* statement takes care of the incrementing.

```
set.seed(10)
#Make a matrix to hold the results
Results <- matrix(NA, nrow = 7, ncol = 2)
dimnames(Results) <- list(c("(Intercept)", "X1", "X2", "X3", "X4", "X5", "RSE"),
c("F1", "F2"))
Results
#> F1 F2
#> (Intercept) NA NA
#> X1 NA NA
#> X2 NA NA
#> X3 NA NA
#> X4 NA NA
#> X5 NA NA
#> RSE NA NA
#Make some data to fit
DF <- data.frame(X1 = rnorm(100), X2 = rnorm(100, 4,2), X3 = rnorm( 100, -8, 2),
X4 = rnorm(100), X5 = rnorm(100, 15,4))
DF$Y <- DF$X1 * 3.33 + rnorm(100, 0,2) + DF$X3 * 9.1+ rnorm(100, 0,2) -
DF$X5 * 0.97 + rnorm(100, 0,3) + 6.78
#Do the fit and store the result in the Results matrix
FIT <- lm(Y ~ ., data = DF)
FITaic <- MASS::stepAIC(FIT,
steps=10000, direction="both", validation = "CV", k=3.5)
#> Start: AIC=299.4
#> Y ~ X1 + X2 + X3 + X4 + X5
#>
#> Df Sum of Sq RSS AIC
#> - X2 1 5.7 1624.0 296.25
#> - X4 1 14.6 1633.0 296.80
#> <none> 1618.3 299.40
#> - X1 1 1279.7 2898.0 354.16
#> - X5 1 1823.5 3441.8 371.36
#> - X3 1 29661.7 31280.0 592.06
#>
#> Step: AIC=296.25
#> Y ~ X1 + X3 + X4 + X5
#>
#> Df Sum of Sq RSS AIC
#> - X4 1 15.2 1639.2 293.68
#> <none> 1624.0 296.25
#> + X2 1 5.7 1618.3 299.40
#> - X1 1 1274.0 2898.0 350.66
#> - X5 1 1862.8 3486.8 369.16
#> - X3 1 29987.5 31611.5 589.61
#>
#> Step: AIC=293.68
#> Y ~ X1 + X3 + X5
#>
#> Df Sum of Sq RSS AIC
#> <none> 1639.2 293.68
#> + X4 1 15.2 1624.0 296.25
#> + X2 1 6.3 1633.0 296.80
#> - X1 1 1287.1 2926.3 348.13
#> - X5 1 1856.3 3495.6 365.91
#> - X3 1 29975.6 31614.8 586.12
vec1 <- c(FITaic$coefficients, RSE = (sum(FITaic$residuals^2)/FITaic$df.residual)^0.5)
vec1
#> (Intercept) X1 X3 X5 RSE
#> 6.0960822 3.8315487 9.0471842 -0.9682519 4.1322434
Results[names(vec1), 1] <- vec1
Results
#> F1 F2
#> (Intercept) 6.0960822 NA
#> X1 3.8315487 NA
#> X2 NA NA
#> X3 9.0471842 NA
#> X4 NA NA
#> X5 -0.9682519 NA
#> RSE 4.1322434 NA
```

^{Created on 2020-01-09 by the reprex package (v0.3.0)}