Get month year from date in R

In Order to get month year from date in R we will be using Format() function. Let’s see how to get month year from date in R with an example.

Let’s first create the dataframe.

### Create Data Frame
df1 = data.frame (
  Name =c('Annie','Catherine','Teresa','Peterson','Richard','joe'),
  Date_of_birth = as.Date(c('1995-06-16','1991-04-19','1993-07-22','1990-03-26','1991-05-12','1992-09-13')))
df1

dataframe df1 will be

Get month year from date in R 1

 

Get month year from date in R

Month year is extracted from Date_of_birth column using Format() functions as shown below.

df1$month_year <-format(df1$Date_of_birth,'%Y-%m')
df1

So the resultant data frame has a column month_year with month year extracted from date.

Get month year from date in R 2

 

                                                                                                               

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts