# Re: [R] Simple 'frequency' function?

From: Marc Schwartz <MSchwartz_at_medanalytics.com>
Date: Sat 10 Jul 2004 - 01:50:35 EST

On Fri, 2004-07-09 at 10:43, Dan Bolser wrote:
> On Fri, 9 Jul 2004, Uwe Ligges wrote:
>
> >Dan Bolser wrote:
> >
> >> Hi, I have designed the following function to extract count frequencies
> >> from an array of integers. For example...
> >>
> >> # Tipical array
> >> x <- cbind(1,1,1,1,1,2,2,2,2,3,3,3,3,4,5,6,7,22)
> >>
> >> # Define the frequency function
> >> frequency <-
> >> function(x){
> >> max <- max(x)
> >> j <- c()
> >> for(i in 1:max){
> >> j[i] <- length(x[x==i])
> >> }
> >> return(j)
> >> }
> >>
> >> fre <- frequency(x)
> >> plot(fre)
> >>
> >> How can I ...
> >>
> >> 1) Make this a general function so my array could be of the form
> >>
> >> # eats!
> >> x <- cbind( "egg","egg","egg","egg","ham","ham","ham","ham","chicken" )
> >>
> >> fre <- frequency(x)
> >> plot(fre)
> >>
> >> 2) Make frequency return an object which I can call plot on (allowing the
> >> prob=TRUE option).
> >
> >
> >See ?table:
> >
> > table(x)
> > plot(table(x))
> > plot(table(x) / sum(table(x)))
> >
>
> Sorry, why does
>
> plot(table(x),log='y')
>
> fail?
>
> I am looking at count/frequency distributions which are linear on log/log
> scales.

Presumably you are getting the following:

"ham","ham","ham","chicken" )
> plot(table(x),log='y')
Error in plot.window(xlim, ylim, log, asp, ...) :

Infinite axis extents [GEPretty(0,inf,5)] In addition: Warning message:
Nonfinite axis limits [GScale(-inf,0.60206,2, .); log=1]

The problem here is that the range for the default y axis is being set to limits that cannot be used on a log scale.

If you review the code for plot.table(), which is the method that will be used here, you see the function definition as follows:

> graphics:::plot.table

function (x, type = "h", ylim = c(0, max(x)), lwd = 2, xlab = NULL,

ylab = NULL, frame.plot = is.num, ...)

Note that the default ylim is set to have a min value of 0, which of course you cannot have on a log scale.

plot(table(x), log = "y", ylim = range(table(x)))

or otherwise explicitly define the y axis range, such that the min value is >0.

Note also that the default plot type here is 'h', which will result in a histogram type of plot using vertical lines. If you want a scatterplot type of graphic, use:

plot(table(x), log = "y", ylim = range(table(x)), type = "p")

HTH, Marc Schwartz

R-help@stat.math.ethz.ch mailing list