[18927] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] Frequency counts (Summary)

daemon@ATHENA.MIT.EDU (Steve Bousquin)
Thu Aug 12 13:37:04 1999

Message-Id: <37B304BA.DF0496FB@sfwmd.gov>
Date: Thu, 12 Aug 1999 13:30:34 -0400
From: "Steve Bousquin" <sbousqu@sfwmd.gov>
Reply-To: sbousqu@sfwmd.gov
Mime-Version: 1.0
To: "s-news@wubios.wustl.edu" <s-news@wubios.wustl.edu>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Several requests later, here is a summary.  Responses,
without names, are separated by asterisks.

Steve


********************

Is there an S-Plus function that will count the number of
unique values or text items in a column, and give the number
of                 occurrances of each alongside the value
or text ? 

E.g., for a column that reads: 

At 
B 
A 
A 
B 
A 
A 
C 

I want output that reads:

A     3                         
At    1 
B     2 
C     1

**************

table()

**************

Hi Steve: "table()" does almost exactly what you want. It
will return a
vector of counts, the "names" of the vector holding the
values. So if you
really needed, say, a data.frame with two columns as you
describe, you might
do this:

my.t <- table (my.vec)
data.frame (Value = names(my.t), Counts = my.t)

**************

Yes--use the command table(matrix), where "matrix" contains
the data you wanted 
sorted and counted.

***************

FYI - there are FOUR A's in your sample data not three

the below provides what you're after, albeit in a loop

>tr <- c("At","B","A","A","B","A","A","C")
>for (i in 1:length(unique(tr)))
cat(unique(tr)[[i]],length(tr[tr==unique(tr)[[i]]]),"\n")

At 1
B 2
A 4
C 1

*****************

There are actually 4 values of A in your column.  You could
use

textvar<-c("A", "A", "A", "B", "B", "D")

table(textvar)

 A B D 
 3 2 1

******************

Use table(factor(data))

******************

> x_c("At","B","A","A","B","A","A","C") 
> table(x)
 A At B C 
 4  1 2 1

*******************

Try

test<-as.factor(c("At","B","A","A","B","A","A","C"))

> summary(test)
         A At B C 
         4  1 2 1

*******************

Steve - I believe table does what you want. BTW your example
had 4
"A"s....


ju<-c("At","B","A","A","B","A","A","C")
> 
> table(ju)
 A At B C 
 4  1 2 1

********************

tapply and length will do what you want
> dummy
[1] "At" "B"  "A"  "A"  "B"  "A"  "A"  "C" 

>tapply(dummy, dummy, length)
 A At B C 
 4  1 2 1

*********************

Use table():

    > x<- c("At","B","A","A","B","A","A","C")
    > table(x)
    A At B C 
    4  1 2 1

table() takes any number of arguments and will make a
multiway
table, one dimension for each argument.  E.g., the following
counts the number of each kind of transition in your
sequence:

    > table(x[-length(x)], x[-1])
    > table(x[-length(x)], x[-1])
       A B C 
     A 2 1 1
    At 0 1 0
     B 2 0 0

********************************

Quite simple, use the table() function, e.g.,

> example<-c("At","B","A","A","B","A","A","C")
> example
[1] "At" "B"  "A"  "A"  "B"  "A"  "A"  "C" 
> table(example)
 A At B C 
 4  1 2 1
> 

**********************************

 Check out the table() function.

**********************************

Try using "table()" w/ a vector, e.g.,

> x
[1] "At" "B"  "A"  "A"  "B"  "A"  "A"  "C" 
> table(x)
 A At B C 
 4  1 2 1

**********************************

Hi. 
Denote by  x  the data vector, 
then table(x) produces what you asked for.  

If you want to separate the unique values and the counts 
then do

tx<-table(x)
print(tx)                       # shows the frequency table
print(as.vector(tx))            # the frequencies only
print(as.numeric(names(tx)))    # the unique x-values

***********************************

table()

***********************************

try table()

***********************************

You have two questions disguised as one, namely

1. How do I produce a labelled frequency vector for a given
vector of any
type?

 and 

2. How do I display the result as a vertical table with
labels along the
rows?

The first is easy, use table()

> freq <- table(vec)

The second question is actually a shade more tricky, but
only a shade.  One
way to do it is

> data.frame(Freq = as.vector(freq), N = names(freq), row.names = "N")

******************************

Try something using split and lapply:

   unlist(lapply(split(x, x), length))

E.g.,

   x <- sample(LETTERS[1:5], 5, T)
   print(x)
   [1] "B" "E" "A" "E" "D" "B" "C" "B" "A" "D"

   print(unlist(lapply(split(x, x), length)))
    A B C D E 
    2 3 1 2 2

Hope that helps.

********************************

Yes, of course there is. Use the function table.
For your example:

> x <- c("At", "B", "A", "A", "B", "A", "A", "C")

> table(x)
 A At B C 
 4  1 2 1

gives the correct answer.

>> I want output that reads:
>> 
>> A     3                         
>> At    1 
>> B     2 
>> C     1

If you want this result, you have to write your own
function,
which gives wrong answers ;-)

********************************

I wrote this function that produce frequency tables...

"fTable"<-
function(x)
{
        n <- length(x)
        summ <- summary(x)
        nval <- ifelse(length(summ) == 7, n - summ[7], n)
        tab <- table(x)
        Value <- names(tab)
        Freq <- codes(tab)
        Perc <- round(Freq/n, 3)
        Valid <- round(Freq/nval, 3)
        Cum <- round(cumsum(Freq)/nval, 3)
        table <- data.frame(Value, Freq, Perc, Valid, Cum)
        if(length(summ) == 7)
                miss <- codes(summ[7])
        else miss <- 0
        List <- list(table, miss)
        cat("\n * Frequency Table * \n\n")
        print(table)
        cat("\n")
        print(summ)
        cat("\n")
        cat(paste("Total cases ", n, "  Valid cases ", nval,
" (", format(nval/
                n * 100, digits = 1, nsmall = 2), "%)  ", " 
Missing cases ", 
                miss, " (", format(miss/n * 100, digits = 1,
nsmall = 2), 
                "%) \n", sep = ""))
        invisible(List)
}

************************************

Thanks for the large response.  Several people suggested
that this could not be done without a custom function, and a
number of people sent interesting functions.  I can
summarize if anyone is interested.  However, 


table(matrix(data))


worked fine for this purpose.

*************************************

This summary is somewhat misleading.  The matrix() call in
the suggested
summary makes no difference at all: i.e., table(data) will
do the same thing
as table(matrix(data)).  The call to matrix turns the vector
into an n x 1
matrix by default, becuase no values have been supplied for
the arguments
nrow= or ncol=, but that transformation has absolutely no
effect on the
table call. 

Because the vector data contains character data and not a
category (or
factor in older versions of S), the explicitly correct
answer would be

        table(category(data))  or equivalently  
table(factor(data))

However, because table() examines its arguments and calls
category() for
those that are not already categorical, the explicit call to
category() or
factor() is unnecessary and, thus, simply

        table(data)

works fine.  Again, however, the matrix() call has no
logical or actual
advantage here.

********************************

Yes, that should have said table(matrix), or table(data),
where the thing in parentheses is the data object.

*********************************


 
=======================================
Steve Bousquin

Kissimmee River Restoration Division
South Florida Water Management District
West Palm Beach, FL

sbousqu@sfwmd.gov
=======================================
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post