# [R] Cumulative percentage by unit of time

Phil Spector spector at stat.berkeley.edu
Wed Dec 15 20:53:34 CET 2010

```Sebastien -
Since you didn't provide a reproducible example, I may
be off track, but here's how I'd handle the problem.

Suppose the data set with nest_id, LD, and FD is called mydata.

First create a function to find the percentage of active nests for
a given time:

> pctact = function(time)100*sum(mydata\$LD <= time & mydata\$FD >= time)/nrow(dat)

Now create the vector of dates you're interested in

> dts = 60:273

Finally use sapply to evaluate the percentage at each date:

> answer = data.frame(dts,sapply(dts,pctact))

If this isn't what you're looking for, a reproducible example

- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu

On Wed, 15 Dec 2010, Sébastien Rioux wrote:

> Hi All,
>
> I've been struggling on that one for a couple of days. I've tried several
> things, but nothing is worth publishing.
>
> I'm working with a bird nests data frame for which I have a laying date (LD)
> and a fledgling date (FD) (in calendar Julian date) by nest id. For the
> period spanning between LD and SD, the nest is termed "active". Each nest id
> occur once in the data frame.
>
> How could I get the cumulative percentage of active nests by date within a
> specified time frame ? Let's say from Julian date 60 to 273 ?
>
> I would like the results to look like this :
>
> Julian_date  prct_ active
> 60                      0
> 61                      5
> 62                      10
> 63                      25
> ...
>
> Here is an example of the data frame
>
> nest_id     LD     FD
>   1    193    219
>   2    131    159
>   3    196    221
>   4    152    179
>   5    136    164
> ?
>
> Cheers,
>
> Sebastien
>
> 	[[alternative HTML version deleted]]
>
>
```