Desired output: 2 columns
predictors mycor
x1 -0.077
x2 -0.266
x3 0.086
x4 -0.037
x5 -0.201
x6 0.055
x7 -0.041
x8 -0.306
x9 0.020
x10 0.038
x11 0.444
11 rows
The following code runs just fine if mycor is entered in manually. But if I try to use mycor created within the loop for(in in 1:11) only the last entry .444 is output. See r code below. cbind and data.frame commands are both giving similar results.
Output:
predictors mycor
x1 0.444
x2 0.444
x3 0.444
x4 0.444
x5 0.444
x6 0.444
x7 0.444
x8 0.444
x9 0.444
x10 0.444
x11 0.444
11 rows
for(i in 1:11){
v <- paste("x",i,sep="")
v2 <- i
mycor <- round(cor(df2[[v]], df2$y2),3)
}
predictors <- c( "x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11")
mycor <- c(-0.077,-0.266, 0.086,-0.037,-0.201,0.055,-0.041,-0.306,0.020,0.038, 0.444)
corSummary <- cbind(predictors,mycor)
corSummary
data.frame(predictors,mycor)
What is happening here?