title: How can I count the current people in a group from a list of start and end dates in R
body: <p>Or rather, How can I do it better than I have fudged.</p>
  
  <p>I have a dataframe with names and start and end dates in a group. I want to produce a dataframe with the number of people in the group over time.  Note, some people haven't left yet (end date is NA)</p>
  
  <p>Here's an example dataset</p>
  
  <pre><code>foo&lt;-data.frame(name=c("Bob","Sue", "Richard", "Jane"), 
          Start=as.POSIXct(c("2006-03-23 GMT", "2007-01-20 GMT", "2007-01-20 GMT", "2006-03-23 GMT")),
          End=as.POSIXct(c("2009-01-20 GMT", "NA", "2006-03-23 GMT", "NA")))
  </code></pre>
  
  <p>Here I create a dataframe with dates covering the range I want.  This feels very dirty. </p>
  
  <pre><code>daterange&lt;-data.frame(date=as.POSIXct(
                  paste(
                          rep(2006:2009, each=12), 
                          "-", 
                          rep(01:12, times=4),
                          "-", 
                          1,
                          " GMT", 
                          sep="")
                          )
                  )
  
  #cheat by setting NAs to soemthing far away
  foo$End[is.na(foo$End)]&lt;-as.POSIXct(Sys.time())+(365*24*60*60)
  </code></pre>
  
  <p>Now I use ddply to produce the result.</p>
  
  <pre><code>ddply(.data=daterange, .variable="date", function(df) {
              result=nrow(subset(foo, Start&lt;df$date &amp; End&gt;df$date))
              return(result)
          })
  </code></pre>
  
  <p>There must be an easier way ?</p>