I am a beginner in R, so excuse my stupid question. I am running a nested loop and want to extract results for the outer and inner loop in a particular format. This is the code I am using -
Res2<-NULL
Res1<-NULL
for (k in 2:ncol(df1)){
for (i in 2:ncol(df2)){
model<-lm(df1[,k]~df2[,i], na.action=na.exclude, data=df2)
Res1<-rbind(Res1, coef(summary(model))[2,])
}
Res2<-cbind(Res2, Res1)
}
Here I am running a linear regression for a number of dependent (response) variables of df1 (from column 2 to the end) against a list of independent variables in df2 (from column 2 to the end). I want to row bind coefficients of the ith iteration into a list, let's call it "Res1", and then want to column bind the next subsequent lists for kth iteration. However, with this code, I get a long list of results where the outer loop also adds results in rows, but not in the column.
A simple layout of the final results file that I want to extract would be something like this - where results of ith iteration will be added in rows and results of kth iteration will be added in columns. Thank you.
K1 ................> K2 ................>
beta Std. Er t val P beta Std. Er t val P
i [1,]
[2,]