Get year from date in R

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

 

Get year from date in R:

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


df1$birth_year <- as.numeric(format(df1$Date_of_birth,'%Y'))
df1

So the resultant data frame has a column birth_year with year extracted from date

Get 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