I am doing a simple linear regression using a nested for loop. I am plotting all ys versus a particular x at a time.
I am new to R, there is something wrong with the statistics code, I am getting this error:

Error in summary1[i, j] <- summary(lm(y[, j] ~ x[, i])) :
incorrect number of subscripts on matrix

Here is the code:

X <-
mtcars[, c("mpg",
"cyl",
"disp",
"hp")]
x = data.matrix(X)
Y <-
mtcars[, c("drat",
"wt",
"qsec",
"vs",
"am")]
y = data.matrix(Y)
for (i in 1:ncol(x)) {
summary1 <- list()
summary2 <- list()
correlation <- list()
confidence <- list()
for (j in 1:ncol(y)) {
summary1[j,i] <- summary(lm(y[, j] ~ x[, i]))
summary2[j,i] <- summary.aov(lm(y[, j] ~ x[, i]))
correlation[j,i] <- cor(y[, j], x[, i])
confidence[j,i] <- confint(lm(y[, j] ~ x[, i]))
}
}
summary1
summary2
correlation
confidence

You are trying to index empty lists as if they are two dimensional objects. You should look into the difference between indexing a list with [ ] and with [[ ]]. I suggest you use nested lists as shown below. I named the list elements, which is not required but I think would make the output easier to understand.