I am looping over a list of dataframes in R and want to use their names as part of the filename I save my plots under.
The code below is my attempt at iterating through dataframes, plotting their first column (var1) versus their second (var2) and then saving the plot.
first.data = data.frame( var1 = 1:4, var2 = 5:8 );
second.data = data.frame( var1 = 9:12, var2 = 13:16 );
for ( dataFrame in list(first.data, second.data) ) {
plot( dataFrame[["var1"]], dataFrame[["var2"]] );
dev.copy( pdf, paste( dataFrame, "_var1_vs_var2.pdf", sep="" ) );
dev.off();
}
I expect this loop to produce PDF files with filenames of the form "first.data_var1_vs_var2.pdf" but instead the name of the data frame is replaced with the first column in the frame and so I get something like "c(1, 2, 3, 4)_var1_vs_var2.exchemVbuffer.pdf".