# R: Calculating mean for where the smaller numbers are 'bigger'

Let's say I have calculated what day does an animal gives birth if it was pregnant on 1st Oct each year. In Julian days, 1st Oct is 274.

# some sample data

years <- c(1980:1990)
pregnancy.date <- rep(274, times=11)
delivery.date <- c(360,362,363,1,5,2,345,6,7,8,9)

dat <- as.data.frame(cbind(years,pregnancy.date,delivery.date))


Here, delivery.date 1,5,2,6,7,8,9 means that the infant was born next year.

For example, for the year 1983, if pregnancy happend on 274 day, the infant was born on 1st Jan of 1984. Similary, for 1987, the infant was born on 6th day of 1988. Hope this is clear.

Now I want to calculate the mean delivery date across the 1980-1990 period.

If I do

mean(dat\$delivery.date)


my mean comes out to be 133 which is wrong since for a given year, most of the birth happens between December of this year and January of next year.

I was wondering what would be the best way to represent the mean delivery date considering in this context the small dates (1,5,2,6,7,8,9) are in fact "bigger" or imply more longer time for delivery.

• To close-voters - the fundamental problem here seems to be to do with handling time data, which is on-topic here, regardless of the R code. Dec 18, 2017 at 18:03

Why not simply add any 365 to any number under or equal to 10?

You can then transform the data to represent number of days until delivery, regardless of the year of delivery (which seems to be what you're looking for).

> ddate <- c(360,362,363,1,5,2,345,6,7,8,9)
> ddate2 = ddate
> ddate2[ ddate2 <= 10 ] = ddate2[ ddate2 <= 10 ] + 365

# Result, before and after adding:
> ddate
[1] 360 362 363   1   5   2 345   6   7   8   9
> ddate2
[1] 360 362 363 366 370 367 345 371 372 373 374

# Mean:
> mean( ddate2 )
[1] 365.7273

• Okay. That sounds more straightforward. Dec 18, 2017 at 14:01