2

I have data on egg_number per day per breeding pair (Parents). I am trying to determine the average amount of time (in days) between the appearance of eggs, grouped by Parents, from the below data "egg_output1".

So basically the average difference between row dates, grouped by parents, after the data is ordered chronologically. Is this possible??

   Tank     Parents                         date            egg_number
1: P3-T25   DON_AGEM_031FXDON_AGEM_036M     2017-06-03      2
2: P3-T25   DON_AGEM_031FXDON_AGEM_036M     2017-06-03      1
3: P3-T25   DON_AGEM_031FXDON_AGEM_036M     2017-05-23      1

I have tried using the following code:

as.Date(egg_output1$date)
egg <- egg_output1[order(egg_output1$date),]
ddply(
  egg, 
  c("Parents"), 
  summarize,
  average = mean(diff(date))
)

But this returns NA with the following warnings:

Warning messages:
1: In mean.default(diff(date)) : argument is not numeric or logical: returning NA
2: In mean.default(diff(date)) : argument is not numeric or logical: returning NA
3: In mean.default(diff(date)) : argument is not numeric or logical: returning NA

sample data:

eggs <- data.frame(
  parents = sample(c("005Fx001M", "008Fx006M","028Fx026M"), 10, replace = TRUE),
  date = sample(seq(as.Date('2016/01/01'), as.Date('2017/01/01'), by="day"), 10),
  egg_number = sample(c("1", "2"), 10, replace = TRUE))
  • 1
    Can you include a small sample of your data? – RobertMyles Jun 15 '17 at 22:29
  • Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Jun 15 '17 at 22:32
  • 1
    Try `.(parents),` instead of `c("Parents")`. That worked for me with your sample data, although it would be `.(Parents)` for your first example. – Luke C Jun 16 '17 at 01:11

1 Answers1

0

From Calculating difference row values by group in R

> dt=NULL
> dt$Tank=rep("P3-T25",3)
> dt$Parents=rep("DON_AGEM_031FXDON_AGEM_036M",3)
> dt$Date=c("2017-06-3","2017-06-3","2017-05-3")
> dt$egg_number=c(2,1,1)
> dt=as.data.frame(dt)
> dt
    Tank                     Parents      Date egg_number
1 P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-06-3          2
2 P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-06-3          1
3 P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-05-3          1


library(data.table)

dt=data.table(dt)
setkey(dt,Parents)
library(lubridate)
   > dt$Date=ymd(dt$Date)
> dt[,diff:=c(NA,diff(Date)),by=Parents]
> dt
     Tank                     Parents       Date egg_number diff
1: P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-06-03          2   NA
2: P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-06-03          1    0
3: P3-T25 DON_AGEM_031FXDON_AGEM_036M 2017-05-03          1  -31
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60