How can I extract dates from a times series? Here is a time series:
x = seq (1, 768) myts <- ts(x, start=1982, frequency=24)
Originally I needed to create a vector holding date/time data for the rts
function, The observations start 1982 with 2 measurements per month going till 2013.
In R programming, if you use Sys. Date() function, it will give you the system date. You don't need to add an argument inside the parentheses to this function.
To get the year from a date in R you can use the functions as. POSIXct() and format() . For example, here's how to extract the year from a date: 1) date <- as. POSIXct("02/03/2014 10:41:00", format = "%m/%d/%Y %H:%M:%S) , and 2) format(date, format="%Y") .
If you just want the day as a string, you can use format(as. Date(df$x,format="%Y-%m-%d"), "%d") .
Try:
time(myts)
or perhaps:
library(zoo) as.yearmon(time(myts))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With