Get month from date in R

In Order to get month from date in R we will be using as.numeric () and Format() function. Let’s see how to get month 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 from date in R 1

 

Get month from date in R

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

df1$birth_month <- as.numeric(format(df1$Date_of_birth,'%m'))
df1

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

Get month from date in R 2