Alex
Alex

Reputation: 117

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)

Upvotes: 5

Views: 6783

Answers (5)

Truong Nhat Minh
Truong Nhat Minh

Reputation: 207

Assume that you have this data frame:

dat = data.frame(daynumber = c(1,5,6,0))

Create a data frame that show the corresponding number to the day

df <- data.frame(Number = 0:6,
                 Day = weekdays(x=as.Date(seq(7), origin="2017-10-01")))

Use match to match the value

dat$daynumber = df$Day[match(dat$daynumber,df$Number)]

Upvotes: 0

DeduciveR
DeduciveR

Reputation: 1702

A bit late, but I was searching to find the same solution and figured there'd be someway to do this in lubridate. lubridate codes days 1 thru 7 but there's still a simple solution:

library(lubridate)

df <- data.frame(day_number=0:6) %>% # Your data
  mutate(t_day_number = day_number +1) %>% # Add 1 to your existing day number
  mutate(day_of_week  = wday(t_day_number, label = TRUE)) # Use wday and switch on labels

  day_number t_day_number day_of_week
1          0            1         Sun
2          1            2         Mon
3          2            3         Tue
4          3            4         Wed
5          4            5         Thu
6          5            6         Fri
7          6            7         Sat

You can use select thereafter to remove the columns you don't want.

Upvotes: 5

Robert Tan
Robert Tan

Reputation: 674

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")

Upvotes: 5

rcs
rcs

Reputation: 68809

Treat the column as a factor with day name as label:

x <- data.frame(wday=rep(0:6, 2))
x$wday_name <- factor(x$wday, levels=0:6,
                      labels=c("Sunday", "Monday", "Tuesday", "Wednesday",
                               "Thursday", "Friday", "Saturday"))
x
#    wday wday_name
# 1     0    Sunday
# 2     1    Monday
# 3     2   Tuesday
# 4     3 Wednesday
# 5     4  Thursday
# 6     5    Friday
# 7     6  Saturday
# 8     0    Sunday
# 9     1    Monday
# 10    2   Tuesday
# 11    3 Wednesday
# 12    4  Thursday
# 13    5    Friday
# 14    6  Saturday

If you need a character column, use as.character afterwards.

Upvotes: 3

Luis Antolin
Luis Antolin

Reputation: 144

If don't want to install any package, there is a good solution:

First let's create a data.frame with your day system, with random variables, just for example:

dnum<-sample(0:6,10,replace=T)
var<-sample(LETTERS,10,replace=T)
df<-data.frame(dnum,var)
> df
   dnum var
1     3   K
2     4   D
3     4   L
4     3   J
5     6   A
6     2   W
7     3   A
8     3   W
9     2   D
10    4   K

You can create a data bank with the day of the week names:

dnom<-c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
do<-0:6
dfo <- data.frame(dnom,do)
> dfo
       dnom do
1    Sunday  0
2    Monday  1
3   Tuesday  2
4 Wednesday  3
5  Thursday  4
6    Friday  5
7  Saturday  6

Now, use a loop to find compare the day numbers in your data.frame, with the data bank, and replace for day of week names.

sub<-c()
for(i in 1 :nrow(df)){
  d<-dfo[dfo$do==df$dnum[i],]
  sub[i]<-as.character(d$dnom[1])
}
df$dnum<-sub

        dnum var
1  Wednesday   K
2   Thursday   D
3   Thursday   L
4  Wednesday   J
5   Saturday   A
6    Tuesday   W
7  Wednesday   A
8  Wednesday   W
9    Tuesday   D
10  Thursday   K

It's not elegant, but I hope that helps you.

Regards.

Upvotes: -2

Related Questions