0
votes

With the help of the following function (original from R - convert POSIXct to fraction of julian day), I convert different starting points of observations, from date format to Julian days.

date <- as.POSIXct(c('2006-12-12 13:00:00', '2008-12-12 12:00:00', '2007-12-12 12:00:00'))

julian_conv <- function(x) { 
  if (is.na(x)) { 
    return(NA)
  }
  else {
    j <-julian(x, origin = as.POSIXlt(paste0(format(x, "%Y"),'-01-01')))
    temp <- unclass(j) 
    return(temp[1] + 1) 
  }
}

julian.days <- sapply(date, julian_conv)

The results:

print(julian.days)
[1] 346.5417 347.5417 346.5000

Then I took the average of those starting points.

mean <- mean(julian.days)
[1] 346.8611

Now I need to convert the average starting point back into a date format (yyyy-dd-mm HH:MM:SS), once for a common year and once for a leap. The question now, how is this possible?

1

1 Answers

1
votes

You can just add the Julian days to a date object:

2006 was a year with 365 days:

as.Date("2006-01-01") + mean(julian.days)

2008 was a leap year:

as.Date("2008-01-01") + mean(julian.days)