Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert day of week number to weekday name in R

Tags:

date

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)
like image 311
Alex Avatar asked Mar 08 '23 21:03

Alex


1 Answers

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")
like image 85
Robert Tan Avatar answered Mar 10 '23 13:03

Robert Tan