Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.GSO.4.21.0003101129530.3610-100000@mail.biostat.washington.edu>
Date: 2000-03-10T19:35:33Z
From: Thomas Lumley
Subject: variable name to hist within apply?
In-Reply-To: <w52ityu1vkl.fsf@psu.edu>

On 10 Mar 2000, Brian P. Flaherty wrote:

> 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'.

Yes, but the argument with which hist is called is newX[,i] (if you look
inside the code for apply)
 
> 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((1:NCOL(toblo.df))[-c(40,52)],
	function (i) hist(toblo.df[,i],main=names(toblo.df)[i]))

should work.  We don't have an analogue of the lisp mapcar function which
takes multiple data arguments (like the data and the names in this case).


	-thomas

Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._