title: Easy way to calculate age period and cohort sample sizes in R
body: <p>In prospective studies, you want to summarize how old your sample is, over which years they were observed, and how long they were observed altogether. These collectively are consider the age, period, and cohort time-scales of the sample.</p>
  
  <p>The easiest way to illustrate is with simulated data:</p>
  
  <p>Suppose these data summarize a cohort of clinic patients with their baseline ages and start and stop date of observation:</p>
  
  <pre><code>set.seed(123)
  n &lt;- 10000
  Obs &lt;- data.frame(
    'age' = sample(seq(40, 80, by=5), n, replace=T),
    'start' = as.Date(n0 &lt;- runif(n, 10000, 12000), origin="1970-01-01"),
    'end' = as.Date(n0 + runif(n, 0, 3652.5), origin="1970-01-01")
  )
  </code></pre>
  
  <p>I want a <code>foo</code> to take vectors </p>
  
  <pre><code>AgeCut &lt;- c(0, 65, Inf)
  Yrcut &lt;- c(0, 2000, Inf)
  DurCut &lt;- c(0, 5, Inf)
  </code></pre>
  
  <p>And cross tabulate the number of individuals who fall into each possible permutation of those values for at least one day. Or, even more complicated-ly, the number of years a person falls into a category. For instance, a person who is 40 when they enter the sample at 1990 and stay in for 30 years would be in the yt65/bf2000/lt5year category for 5 years when they enter yt65/bf2000/gt5year and stay there for another 5 years when they enter yt65/af2000/gt5year for 15 more years and finally ot65/af2000/gt5year</p>
  
  <p>For some reason, this is wracking my brain so heavily I can't calculate the <em>actual</em> desired output, even via some inefficient for loop, but the format and structure would be something like:</p>
  
  <pre><code>        AgeCut             YrCut            DurCut  NumObs
  1 younger than 65    before 2000 less than 5 years    1000
  2    65 and older    before 2000 less than 5 years    1000   
  3 younger than 65 2000 and later less than 5 years    1000
  4    65 and older 2000 and later less than 5 years    1000
  5 younger than 65    before 2000   5 or more years    1000
  6    65 and older    before 2000   5 or more years    1000
  7 younger than 65 2000 and later   5 or more years    1000
  8    65 and older 2000 and later   5 or more years    1000
  </code></pre>