I have a data.frame with ~100 columns and I need a barplot for each column produced and saved in some directory. I am not sure it is possible - so please help me. this is my loop that does not work... vars <- list (substitute (G01_01), substitute (G01_02), substitute (G01_03), substitute (G01_04)) results <- data.frame ('Variable Name'=rep (NA, length (vars)), check.names=FALSE) for (i in 1:length (vars)) { barplot(table(i),xlab=i,ylab="Nuomon?s") dev.copy(png, filename="/my/dir/barplot.i.png", height=600, width=600) dev.off() } questions: Is it possible to use the i somewhere _within_ a file name? (like it is possible in other programming or scripting languages?) Since I hate to type in all the variables (they go from G01_01 to G01_10 and then from G02_01 to G02_10 and so on), is it possible to shorten this list by putting there another loop, applying some programming thing or so? -- Donatas Glodenis http://dg.lapas.info
Donatas G. wrote:> I have a data.frame with ~100 columns and I need a barplot for each column > produced and saved in some directory. > > I am not sure it is possible - so please help me. > > this is my loop that does not work... > > vars <- list (substitute (G01_01), substitute (G01_02), substitute (G01_03), > substitute (G01_04)) > results <- data.frame ('Variable Name'=rep (NA, length (vars)), > check.names=FALSE) > for (i in 1:length (vars)) { > barplot(table(i),xlab=i,ylab="Nuomon?s") > dev.copy(png, filename="/my/dir/barplot.i.png", height=600, width=600) > dev.off() > } > > questions: > > Is it possible to use the i somewhere _within_ a file name? (like it is > possible in other programming or scripting languages?)Oh yes, very easy. two options: 1) Use sprintf, e.g. filename=sprintf("/my/dir/barplot.%d.png",i) 2) Use paste, i.e., filename=paste('/my/dir/barplot.',i,'.png',sep='')> Since I hate to type in all the variables (they go from G01_01 to G01_10 and > then from G02_01 to G02_10 and so on), is it possible to shorten this list by > putting there another loop, applying some programming thing or so?Well sure! Just loop over each column of your data frame. The column names are gotten from the names() function. I don't see a data frame in your code so I assume d is below: for (i in names(d)){ barplot(d[,i],filename=sprintf("/my/dir/barplot.%s.png",i)) } Notice that names(d) returns a character vector, thus i is a string, whereas in my sprintf example in 1) presumed it an int. Also, be sure to read up on the apply family of functions as an alternative to using loops in R code. Jeff -- http://biostat.mc.vanderbilt.edu/JeffreyHorner
Not sure exactly what 'results' is doing there or 'barplot(table(i),...)' does [see ?table] but I think this is sort of what you want to do? ## Variable assignment G01_01 <- 1:10 G01_02 <- 2:6 ## Combine to list* varnames <- paste("G01_",substring(100+1:2,2),sep="") vars <- lapply(`names<-`(as.list(varnames),varnames), function(x) eval(parse(text=x))) print(vars) ## Plotting for( i in 1:length(vars) ) { filenm <- paste("/my/dir/barplot",i,".png",sep="") barplot(...) dev.copy(png,filename=filenm,...) dev.off() } ## **Combining to list, step-by-step ## does the same thing as above digits <- substring(100+1:2,2) varnames <- paste("G01_",digits,sep="") vars <- as.list(varnames) names(varlist) <- vars # convert character string of variable names to # expressions via parse() and evaluate by eval() vars <- lapply(varlist,function(x) eval(parse(text=x))) print(vars) I think in many cases paste() is your answer... --- "Donatas G." <dgvirtual at akl.lt> wrote:> I have a data.frame with ~100 columns and I need a barplot for each column > produced and saved in some directory. > > I am not sure it is possible - so please help me. > > this is my loop that does not work... > > vars <- list (substitute (G01_01), substitute (G01_02), substitute > (G01_03), > substitute (G01_04)) > results <- data.frame ('Variable Name'=rep (NA, length (vars)), > check.names=FALSE) > for (i in 1:length (vars)) { > barplot(table(i),xlab=i,ylab="Nuomon??s") > dev.copy(png, filename="/my/dir/barplot.i.png", height=600, width=600) > dev.off() > } > > questions: > > Is it possible to use the i somewhere _within_ a file name? (like it is > possible in other programming or scripting languages?) > > Since I hate to type in all the variables (they go from G01_01 to G01_10 > and > then from G02_01 to G02_10 and so on), is it possible to shorten this list > by > putting there another loop, applying some programming thing or so? > > -- > Donatas Glodenis > http://dg.lapas.info > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > 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. >____________________________________________________________________________________ that gives answers, not web links.
On 8/4/07, Donatas G. <dgvirtual at akl.lt> wrote:> I have a data.frame with ~100 columns and I need a barplot for each column > produced and saved in some directory. > > I am not sure it is possible - so please help me. > > this is my loop that does not work... > > vars <- list (substitute (G01_01), substitute (G01_02), substitute (G01_03), > substitute (G01_04)) > results <- data.frame ('Variable Name'=rep (NA, length (vars)), > check.names=FALSE) > for (i in 1:length (vars)) { > barplot(table(i),xlab=i,ylab="Nuomon?s") > dev.copy(png, filename="/my/dir/barplot.i.png", height=600, width=600) > dev.off() > } > > questions: > > Is it possible to use the i somewhere _within_ a file name? (like it is > possible in other programming or scripting languages?)(Yes, but) Why are you using dev.copy? See ?png, in particular how the ``page number'' can be encoded in the 'filename' argument for multi-page output. -Deepayan