2

I have a dataframe that looks like following:

df <- data.frame(site=paste0('site', sort(rep(1:5, 20))),
                 date=as.Date(paste0(sample(1:28, 100, replace=T), '/', 
                                     sample(1:12, 100, replace=T), '/', 
                                     2013), 
                              '%d/%m/%Y'), 
                 count=rep(seq(1, 1000, length.out=20), 10))

For each site, I need the earliest date in time count > 500. In in other words, the first date at which the count was >50% of max count.

luciano
  • 13,158
  • 36
  • 90
  • 130

1 Answers1

3

Solutions with plyr.

If you want the first date where count > 500 :

ddply(df, .(site), summarise, date=min(date[count>500]))

If you want the first date where count > 50%*max(count) (for each site) :

ddply(df, .(site), summarise, date=min(date[count>max(count)*0.5]))

Here both give the same result :

   site       date
1 site1 2013-01-15
2 site2 2013-02-04
3 site3 2013-03-13
4 site4 2013-02-04
5 site5 2013-01-07
juba
  • 47,631
  • 14
  • 113
  • 118