[R] frequency tables

Don MacQueen macq at llnl.gov
Mon Jun 20 23:24:23 CEST 2005


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.

-Don

At 11:38 AM -0700 6/20/05, Kevin J Emerson wrote:
>R-masters,
>
>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
>information?
>
>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
>--
>------------------------------------
>------------------------------------
>Kevin J Emerson
>Center for Ecology and Evolutionary Biology
>1210 University of Oregon
>University of Oregon
>Eugene, OR 97403
>kemerson at dakrwing.uoregon.edu
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


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




More information about the R-help mailing list