Convert Date to Julian Day in R (3 Examples)

 

In this tutorial, I’ll demonstrate how to convert a date object to a Julian day in R programming.

Table of contents:

So let’s get started:

 

Creation of Example Data

We use the following data as a basement for this R programming tutorial:

my_date <- as.Date("2023-03-15")          # Create example date
my_date                                   # Print example date
# [1] "2023-03-15"

The previous output of the RStudio console shows that our example data object is a date with the Date class containing the 15th of March 2023.

 

Example 1: Convert Calendar Date to Julian Day Using as.POSIXlt() Function

The R programming syntax below illustrates how to transform a date object to a Julian day using the as.POSIXlt function.

Consider the R code below:

my_julian1 <- as.POSIXlt(my_date)$yday    # Convert date to Julian day
my_julian1                                # Print Julian day
# [1] 73

The previous syntax has created a new data object called my_julian1 that contains the value 73, i.e. the Julian day corresponding to our example date.

Note that the as.POSIXlt function starts counting Julian days from 0.

 

Example 2: Convert Calendar Date to Julian Day Using format() Function

Alternatively to the as.POSIXlt function, we can also use the format function to get the Julian day of a date:

my_julian2 <- format(my_date, "%j")       # Convert date to Julian day
my_julian2                                # Print Julian day
# [1] "074"

Note two differences compared to the output of Example 1:

  • The format function starts counting Julian days from 1 instead of 0.
  • The format function returns a character string instead of a numeric value.

 

Example 3: Convert Calendar Date to Julian Day Using yday() Function of lubridate Package

So far, we have used Base R to convert our date to a Julian day. This section, in contrast, uses the lubridate package for this task.

First, we need to install and load the lubridate package:

install.packages("lubridate")             # Install lubridate package
library("lubridate")                      # Load lubridate

Next, we can apply the yday function to return the Julian day that corresponds to our date:

my_julian3 <- yday(my_date)               # Convert date to Julian day
my_julian3                                # Print Julian day
# [1] 74

As you can see, the ymd function starts counting from 1 and returns a numeric value.

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. I illustrate the topics of this article in the video.

 

 

Furthermore, you may have a look at the other tutorials on my website:

 

In this tutorial you have learned how to get a Julian day from a date in the R programming language.

In this tutorial, I have demonstrated how to transform a single date object. Please note that we could apply the same R codes to vectors or entire data frame columns as well.

If you have any additional questions, kindly let me know in the comments below.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top