[R] Frequency table to histogram

William Dunlap wdunlap at tibco.com
Tue Apr 7 23:35:11 CEST 2009


You could use rep() to turn your frequency table
into a raw data vector and then call hist, which retabulates
the raw data and plots the table with barplot.  That is
useful if you want a histogram on a courser scale than your original
table.  (S+'s hist() function has a freq= argument so
that hist(x,freq=freq) is equivalent to hist(rep(x,freq))),
automating this a bit and possibly saving some memory.  Unfortunately
S+ and R conflict on the meaning of the freq= argument there.)

Another approach is to skip the untabulate/tabulate sequence
and just call barplot using your frequency table as the input.
E.g.,
    temp <- data.frame(Count=c(3,18,81,185,420),
                       Chest=c(33,34,35,36,37))
    with(temp, barplot(Count, names=Chest))
This preserves the breakpoints in the original frequency
table and often makes a prettier looking plot.  If your
frequency table is out of order or has gaps in them you
will need to patch it up.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 

----------------------------------------------------------------
[R] Frequency table to histogram

Luc Villandre villandl at dms.umontreal.ca 
Tue Apr 7 16:30:32 CEST 2009
   Hi Louisa,
   I'm still not entirely sure of what you're trying to do, but I'll
attempt an
   answer to your question nevertheless.
   Let's assume that this frequency table was created from a dataset
that
   comprised only a single column of "Chest" values. If you want to
recreate
   this original column, you'd have to use
   temp1 = rep(temp$Chest,temp$Count) ;
   Then, you could redefine your break points and let the hist()
function count
   the number of data entries in each new category.
   Hope this helps,
   Luc
   Mary Winter wrote:

     Hi,

     Sorry, yes that would be a better idea, Luc!
     Here are the first few lines of the frequency table..

     Count Chest
     1      3    33
     2     18    34
     3     81    35
     4    185    36
     5    420    37

     In my previous email I said the sample size was 100, I just meant
that as
     an example, obviously it's far greater than that..

     Louisa

     > Date: Tue, 7 Apr 2009 10:00:55 -0400
     > From: [1]villandl at dms.umontreal.ca
     > To: [2]statsstudent at hotmail.com
     > CC: [3]r-help at r-project.org
     > Subject: Re: [R] Frequency table to histogram
     >
     > Hi Mary,
     >
     > Could you please provide a copy of the first few lines of your
frequency
     > table (with only relevant columns)? I think this could help many
     > potential contributors understand in more details what you're
trying to
     do.
     >
     > Cheers,
     >
     > Luc
     >
     > Mary Winter wrote:
     > >
     > >
     > > I have read a frequency table in to R called "temp." I now want
to
     create a histogram table from it, but I obviously first have to
expand the
     data - to the sample size of 100. I want to use the command rep(),
but I'm
     not sure how to go about it..I tried using the code:
     > >
     > >
     > >
     > > temp1<-rep(temp$Chest,100)
     > >
     > > hist(temp1)
     > >
     > >
     > >
     > > But this creates a v. odd histogram so I know it must be wrong!
     > >
     > >
     > >
     > > Would be very grateful if any one could help me out!
     > >
     > >
     > >
     > > Mary
     > >
     > >
_________________________________________________________________
     > >
     > >
     > > [[alternative HTML version deleted]]
     > >
     > > ______________________________________________
     > > [4]R-help at r-project.org mailing list
     > > [5]https://stat.ethz.ch/mailman/listinfo/r-help
     > > PLEASE do read the posting guide
     [6]http://www.R-project.org/posting-guide.html
     > > and provide commented, minimal, self-contained, reproducible
code.




More information about the R-help mailing list