[R] how to use by() and hist()

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun May 6 18:07:46 CEST 2001


On Sun, 6 May 2001, David White wrote:

>
> Hello,
>
> I'm using R 1.2.2 on Sun Solaris.
>
> I have  data frame with 4 levels of factor "type". See the example data
> frame below.
>      type            token     variance
> 20   ku n031ku10.10msmeanc  77199422
> 21   ku n031ku11.10msmeanc  55682249
> 22   ku n031ku12.10msmeanc  52003965
> 23   ti n031ti01.10msmeanc  54511040
> 24   ti n031ti02.10msmeanc  58940197
> 25   ti n031ti03.10msmeanc  46918442
>
> I'd like to plot histograms by token type as listed in column 1 above.
> I tried
> by(allvariances[,3], allvariances[,1], hist)
> this worked well, but provided histograms with different numbers of bins
> for each token type.
>
> I also tried
>  by(allvariances[,3], allvariances[,1], hist(breaks=4))
> to specify the number of bins. Hist then complained that I had not
> specified a value for the data to be plotted.

by(allvariances[,3], allvariances[,1], function(x) hist(x, breaks=4))

or

by(allvariances[,3], allvariances[,1], hist, breaks=4)

(see the documentation of the ... argument in ?hist).


> Can anyone offer any pointers?
>
> A related question: I'd like to see subsets of the data based on the
> levels of factor "type". So in the example above, I'd like see, say, all
> observations of type "tu". Any pointers on how to do that?

Just index the data frame, for example

allvariances[allvariances$type %in% "tu", ]

(The reason for not using == is the handling of missing values.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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