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]]
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
would be helpful.
- 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]]
>
>
Hi Sebastien!
How about this:
d<-data.frame(nest,LD,FD)
l<-apply(d,1,function(i){seq(i["LD"],i["FD"],by=1)})
jul<-60:273
active<-sapply(jul,function(i){sum(sapply(l,function(k){i %in% k}))})
ans<-data.frame(jul,perc_act=100*active/length(nest))
ans
When you say cumulative percentage active, do you mean nests active on a given
date or do you also want to include nests that have been active?
Cheers,
Francois Rousseu
Date: Wed, 15 Dec 2010 14:34:04 -0500
From: sebastien.rioux@gmail.com
To: r-help@r-project.org
Subject: [R] Cumulative percentage by unit of time
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]]
______________________________________________ R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal,
self-contained, reproducible code.
[[alternative HTML version deleted]]