Donald Braman
2009-Aug-26 03:08 UTC
[R] simple graph question: manipulating variable names
This is a simple problem that has stumped me: I'm trying to loop through a few dozen variable names in graphs. I've tried various approaches like this: attach(mydata) ivs <- c("oneiv", "anotheriv", "yetanotheriv") dvs <- c("onedv", "anotherdv", "yetanotherdv") for (iv in ivs) { for (dv in dvs) { graphname <- paste(iv, dv, ".png", sep = "") png(file=graphname, width=300, height=300) plot(dv ~ iv, pch=".") lines(loess.smooth(iv, dv), lty=1) dev.off() } } Clearly that doesn't work. I'm not sure how to make R see the iv and dv strings as variables. Advice? Donald Braman phone: 413-628-1221 http://www.culturalcognition.net/braman/ http://ssrn.com/author=286206 http://www.law.gwu.edu/Faculty/profile.aspx?id=10123 [[alternative HTML version deleted]]
On 26/08/2009, at 3:08 PM, Donald Braman wrote:> This is a simple problem that has stumped me: I'm trying to loop > through a > few dozen variable names in graphs. I've tried various approaches > like > this: > attach(mydata) > ivs <- c("oneiv", "anotheriv", "yetanotheriv") > dvs <- c("onedv", "anotherdv", "yetanotherdv") > for (iv in ivs) { > for (dv in dvs) { > graphname <- paste(iv, dv, ".png", sep = "") > png(file=graphname, width=300, height=300) > plot(dv ~ iv, pch=".") > lines(loess.smooth(iv, dv), lty=1) > dev.off() > } > } > > Clearly that doesn't work. I'm not sure how to make R see the iv > and dv > strings as variables. Advice??get cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Donald Braman wrote:> This is a simple problem that has stumped me: I'm trying to loop through a > few dozen variable names in graphs. I've tried various approaches like > this: > attach(mydata) > ivs <- c("oneiv", "anotheriv", "yetanotheriv") > dvs <- c("onedv", "anotherdv", "yetanotherdv") > for (iv in ivs) { > for (dv in dvs) { > graphname <- paste(iv, dv, ".png", sep = "") > png(file=graphname, width=300, height=300) > plot(dv ~ iv, pch=".") > lines(loess.smooth(iv, dv), lty=1) > dev.off() > } > } > > Clearly that doesn't work. I'm not sure how to make R see the iv and dv > strings as variables. Advice? > >Hi Donald, I think the problem is that you are trying to plot the strings that you are using for your filename rather than the elements of "mydata". Try this: for(ivindex in 1:3) { for(dvindex in 1:3) { graphname<-paste(iv[ivindex],dv[dvindex],".png",sep="") png(graphname,width=300,height=300) plot(mydata[,3*iv-dv-1],mydata[,3*iv-dv],pch=".") lines(loess.smooth(mydata[,3*iv-dv-1],mydata[,3*iv-dv],lty=1) dev.off() } } remembering that I have made up the indexing of "mydata" out of thin air. You will have to work out how to index the columns or rows of "mydata" to get the right iv and dv for each pass of the loops. Jim