rdate

Convert day of week number to weekday name in R


I have a column in a dataframe that contains the day number ( 0 through 6, 0=Sunday, 1=Monday, etc) and I need to convert that to the day name. How can I do this?

Sample data:

df <- data.frame(day_number=0:6)

Solution

  • Simple way with dplyr.

    library(dplyr)
    
    df <- data.frame(day_number=0:6)
    
    df$day_number <- recode(df$day_number, 
           "0"="Sunday",
           "1"="Monday",
           "2"="Tuesday",
           "3"="Wednesday",
           "4"="Thursday",
           "5"="Friday",
           "6"="Saturday")