[R] variable name to hist within apply?

Brian P. Flaherty bxf4 at psu.edu
Fri Mar 10 20:20:10 CET 2000


Hello,

After spending sometime trying to generate histograms of all the
numeric variables in a data frame using a for loop, I realized I could
use apply to create the histograms.  This was one of those few moments
when I `see' the alternative to looping.

So, I can generate all the histograms with this command:

apply(toblo.df[,-c(40,52)],2,hist) # 40 and 52 are ID's

When I do this, the name of each histogram is "Histogram of
newX[,i]".  Just out of curiosity, where does `newX[,i]' come from?
The help page for hist, it says the data argument is `x'.

The problem with this naming style is that there are over 100
variables in this data set and it would be nice if the name of each
variable was above the histogram, instead of an `i'.  How do I get the
variable name into the title of the histogram?  I have tried a lot of
things including:

apply(toblo.df[,-c(40,52)],2,hist, main=names(x)[i])
  # error, x not found

apply(toblo.df[,-c(40,52)],2,hist, main="names(x)[i]")
  # the name is "names(x)[i]"

apply(toblo.df[,-c(40,52)],2,hist, main=names(newX)[i])

I also created a vector of the dimnames of toblo.df and then tried
this:

apply(toblo.df[,-c(40,52)],2,hist, main=varnames[i])

but in this case, the title of each histogram is `NA'.  Presumably
this is because there isn't an `i' available even though I know
somewhere apply is passing hist an `i'.

Any pointers on this will be appreciated.  I also will be happy to
summarize any answers I receive via email.  Thanks for your time.


-- 
Brian P. Flaherty
The Pennsylvania State University	office phone:  814/863-5836 
The Methodology Center			fax:  814/863-0000          
S-159 Henderson Bldg.			email:  bxf4 at psu.edu
University Park, PA, 16802		http://www.personal.psu.edu/~bxf4

-- 
      /"\
      \ /     ASCII RIBBON CAMPAIGN
       X        AGAINST HTML MAIL  
      / \ 


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list