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.
Hello, Try x<-data.frame(A=1:5,B=6:10,C=11:15,D=16:20,E=21:26) titles<-c("A","B","C","E") y <- "C" x[ , y] Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Calling-a-column-from-a-matrix-using-a-variable-tp4457466p4457509.html Sent from the R help mailing list archive at Nabble.com.
Possibly Parallel Threads
- help on matrix column removal based on another matrix results
- Subsetting row in single column matrix drops names in resulting vector
- Subsetting row in single column matrix drops names in resulting vector
- How to index a matrix with different row-number for each column?
- Bootstrap and average median squared error