Get Dates from Time Series (ts Object) in R (Example Code)

In this R programming post you’ll learn how to get the dates of a time series object.

Exemplifying Data

x <- ts(1:5, start = 2025)        # Constructing a time series
x
# Time Series:
# Start = 2025 
# End = 2029 
# Frequency = 1 
# [1] 1 2 3 4 5

Example: Applying as.yearmon() Function of zoo Package to Extract Dates from a Time Series

install.packages("zoo")           # Install zoo package
library("zoo")                    # Load zoo
as.yearmon(time(x))               # Extracting dates from time series
# [1] "Jan 2025" "Jan 2026" "Jan 2027" "Jan 2028" "Jan 2029"

Further Resources

Below, you can find some additional resources on topics such as time objects, ggplot2, and descriptive statistics:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top