[R] Name of data.frame as a text string?
Mark Knecht
markknecht at gmail.com
Thu Jul 2 21:28:33 CEST 2009
On 7/2/09, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:
> On 02-Jul-09 19:00:44, Mark Knecht wrote:
> > I've passed a data.frame as an input into a function which does some
> > plotting on a 4x4 matrix of certain parameters within in the
> > data.frame. I'd like to add a small header on top of each plot with
> > the name of the data.frame so that it's clear as I compare these 16
> > things where each on came from.
> >
> > So far I haven't found the right way to get the name of the data.frame
> > as a string which I can use in something like mtext. Is there one? If
> > I put dummy text in, or take the time to pass in the name by hand,
> > then I do get titles just as I'd like, but I'd far rather let the name
> > of the data.frame speak for itself.
> >
> > Thanks,
> > Mark
>
>
> One way to do this (which is how I usually do it) is to set up the
> dataframe name as a string variable, and then use this as required.
>
> For instance:
>
> datafr <- "My1stDF"
> DF <- read.csv(paste(datafr,".csv",sep="")
> {<do things>}
> plot(whatever,main=paste("Data from", datafr),...)
>
> which will read the CSV file whose name corresponds to what you have
> set 'datafr' to, and then put a corresponding header into the plot.
>
> This is a particularly useful technique if you want to loop through
> several datasets, on the lines of
>
> DataFrs <- c("My1stDF", "My2ndDFD", "My3rdDF", "My4thDF")
> for( datafr in DataFrs ) {
> {<stuff like the above>}
> }
>
> Several variants of this kind of approach are possible!
>
> Hoping this helps,
> Ted.
>
Yep, doing it that way is very sensible. I've got the name in my hand
at the point I read the file in. Easy to use this way and probably a
good long-term solution.
Cheers,
Mark
More information about the R-help
mailing list