Get day of the week from date in R

In Order to get day of the week from date in R we will be using weekdays() function. Let’s see how to get day of the week from date with an example, using weekdays() function in R.

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 day of the week from date in R 1

Get day of the week from date in R

Day of the week is extracted from Date_of_birth column using weekdays() function as shown below.

df1$week_day <- weekdays(df1$Date_of_birth)
df1

So the resultant data frame has a column week_day with day of the week extracted from date.

get day of the week 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