Hi, there I am new to R, here is an urgent question. I want to save several graphs by a for loop. But I don't know how to refer the variable in the quotes. Here is my code. g<-c("g1","g2","g3","g4","g5","g6","g7","g8","g9") for (u in 1:9) {pdf("g[u]".pdf") + plot(1,1, xlim=range(x), ylim=c(-5,5), type='n', xlab ="Femur length (mm)", ylab = "Z-Score") + mtext("g[u]", side = 4) + for (i in z) { + lines(x,(log(i)-(m[u]*log(x)+c[u]))/r[u]) + miny<-(log(i)-(m[u]*log(max(x))+c[u]))/r[u] + maxx <- exp((log(i)-c[u]+5*r[u])/m[u]); + if (miny > -5) {text(max(x),miny+0.3,i) + }else {text(maxx+2, -5+0.3,i)} + } + dev.off() + } In the second line, I want to refer the variable 'g[n]' to the file name and fifth line i also want to refer the variable as a note. How should I do? Thank you very much! -- Sincerely, Jinggaofu Shi 4029 Spring Garden Street Philadelphia, PA 19104 Email: shijinggaofu at gmail.com Phone: 215-847-9145 [[alternative HTML version deleted]]
1. Please spend some time with an R tutorial or two to become familiar with R basics. 2. Please do not post in HTML. This is a plain text email list. 3. See e.g. ?sprintf or ?paste for your problem. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Mar 2, 2016 at 2:52 PM, Jinggaofu Shi <js3786 at drexel.edu> wrote:> Hi, there > I am new to R, here is an urgent question. > I want to save several graphs by a for loop. But I don't know how to refer > the variable in the quotes. > Here is my code. > > g<-c("g1","g2","g3","g4","g5","g6","g7","g8","g9") > for (u in 1:9) {pdf("g[u]".pdf") > + plot(1,1, xlim=range(x), ylim=c(-5,5), type='n', xlab ="Femur length > (mm)", ylab = "Z-Score") > + mtext("g[u]", side = 4) > + for (i in z) { > + lines(x,(log(i)-(m[u]*log(x)+c[u]))/r[u]) > + miny<-(log(i)-(m[u]*log(max(x))+c[u]))/r[u] > + maxx <- exp((log(i)-c[u]+5*r[u])/m[u]); > + if (miny > -5) {text(max(x),miny+0.3,i) > + }else {text(maxx+2, -5+0.3,i)} > + } > + dev.off() > + } > > In the second line, I want to refer the variable 'g[n]' to the file name > and fifth line i also want to refer the variable as a note. How should I > do? Thank you very much! > > -- > Sincerely, > Jinggaofu Shi > 4029 Spring Garden Street > Philadelphia, PA 19104 > Email: shijinggaofu at gmail.com > Phone: 215-847-9145 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Jinggaofu, Try this: for(u in 1:9) { pdffile<-paste(g[u],".pdf",sep="") pdf(pdffile) ... mtext(g[u],side=4) If you index the vector g, it will return one or more character strings. Jim On Thu, Mar 3, 2016 at 9:52 AM, Jinggaofu Shi <js3786 at drexel.edu> wrote:> Hi, there > I am new to R, here is an urgent question. > I want to save several graphs by a for loop. But I don't know how to refer > the variable in the quotes. > Here is my code. > > g<-c("g1","g2","g3","g4","g5","g6","g7","g8","g9") > for (u in 1:9) {pdf("g[u]".pdf") > + plot(1,1, xlim=range(x), ylim=c(-5,5), type='n', xlab ="Femur length > (mm)", ylab = "Z-Score") > + mtext("g[u]", side = 4) > + for (i in z) { > + lines(x,(log(i)-(m[u]*log(x)+c[u]))/r[u]) > + miny<-(log(i)-(m[u]*log(max(x))+c[u]))/r[u] > + maxx <- exp((log(i)-c[u]+5*r[u])/m[u]); > + if (miny > -5) {text(max(x),miny+0.3,i) > + }else {text(maxx+2, -5+0.3,i)} > + } > + dev.off() > + } > > In the second line, I want to refer the variable 'g[n]' to the file name > and fifth line i also want to refer the variable as a note. How should I > do? Thank you very much! > > -- > Sincerely, > Jinggaofu Shi > 4029 Spring Garden Street > Philadelphia, PA 19104 > Email: shijinggaofu at gmail.com > Phone: 215-847-9145 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.