Re: [R] frequency tables

From: Don MacQueen <>
Date: Tue 21 Jun 2005 - 07:24:23 EST

Here's an example of how to replicate rows according to a count that is provided in one of the variables.

> foo <- data.frame(id=letters[1:3],cl=LETTERS[1:3],n.yes=c(3,5,2))
> foo

   id cl n.yes

1  a  A     3
2  b  B     5
3  c  C     2

> cbind(foo[rep(1:nrow(foo),foo$n.yes),c('id','cl')],res=rep(1,sum(foo$n.yes)))
id cl res

1 a A 1
1.1 a A 1
1.2 a A 1
2 b B 1
2.1  b  B   1
2.2  b  B   1
2.3  b  B   1
2.4  b  B   1

3 c C 1
3.1 c C 1

I assumed your third column is the frequency of "yes" events; I don't know w here you meant the frequency of "no" events to come from.


At 11:38 AM -0700 6/20/05, Kevin J Emerson wrote:
>I have a problem that I have been working on for a while and it seems
>that there may be a simple solution that I have yet to figure out, so I
>thought that I would venture to post to the help list.
>Let's say there was a data.frame with three vectors, two that are
>factors identifying the data, and one that holds the frequency of
>occurrence (the events are binary, yes or no). I would like to perform
>logistic regression on this data, and it seems that I need a vector of
>0s and 1s for input into lrm. How might I convert between a frequency
>table and a vector of binary data while still maintaining all identifier
>I have thought about using the rep command over and over again and
>basically building the data.frame "by hand" but that seems long and
>tedious. Is there a quick and dirty way of doing this?
>Thanks in advance!
>Kevin J Emerson
>Center for Ecology and Evolutionary Biology
>1210 University of Oregon
>University of Oregon
>Eugene, OR 97403
> mailing list
>PLEASE do read the posting guide!

Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Tue Jun 21 07:27:28 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:54 EST