I have a yearmon object:
require(zoo)
date1 <- as.yearmon("Mar 2012", "%b %Y")
class(date1)
# [1] "yearmon"
How can I extract the month and year from this?
month1 <- fn(date1)
year1 <- fn(date1)
What function should I use in place of fn()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the
format()method for objects of class"yearmon". Here is your example date (properly created!)Then we can extract the date parts as required:
These are returned as characters. Where appropriate, wrap in
as.numeric()if you want the year or numeric month as a numeric variable, e.g.See
?yearmonand?strftimefor details – the latter explains the placeholder characters you can use.