[R] Name of data.frame as a text string?

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu Jul 2 21:21:47 CEST 2009


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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 02-Jul-09                                       Time: 20:21:43
------------------------------ XFMail ------------------------------




More information about the R-help mailing list