[R] Calling a column from a matrix using a variable

rgm robinmunshaw at gmail.com
Thu Mar 8 20:42:50 CET 2012


Hi there,
I'm having some difficulty using a variable to call a column from a matrix.
I am trying to analyze several columns against each other using a for loop
in the following way:

x<-data.frame(A=1:5,B=6:10,C=11:15,D=16:20,E=21:26)
titles<-c("A","B","C","E")

for( i in 1:length(titles)){
  y<-titles[i]
  regression<-lm(x$D~x$"y")
  print(summary(regression)$r.squared)
  }

So that I get a list of the r-squared values for a regression of column D
against all the other columns individually.
However, I can't figure out how to insert the changing value for y into the
lm(x$D~x$"y") function without the command trying to call "column y" from
the matrix.

Thanks for any help!





--
View this message in context: http://r.789695.n4.nabble.com/Calling-a-column-from-a-matrix-using-a-variable-tp4457466p4457466.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list