How to convert days in a week to number in R data frame column?


To convert days in a week to number in R data frame column, we would need to convert the column into a factor by defining the weekdays as the levels and then read that column as integer.

If we provide the correct sequence of weekdays during conversion then Monday will be converted to 1. Check out the below Examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

Week_Days<-
sample(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),20,replace=TRUE)
df1<-data.frame(Week_Days)
df1

The following dataframe is created

   Week_Days
1     Friday
2     Monday
3    Tuesday
4   Saturday
5   Thursday
6    Tuesday
7     Monday
8   Thursday
9   Thursday
10    Friday
11  Thursday
12    Sunday
13  Thursday
14   Tuesday
15 Wednesday
16    Sunday
17    Friday
18 Wednesday
19    Sunday
20  Thursday

To convert Week_Days in df1 to number and reading them in a different column on the above created data frame, add the following code to the above snippet −

Week_Days<-
sample(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),20,replace=TRUE)
df1<-data.frame(Week_Days)
df1$Days_Number<-
as.integer(factor(df1$Week_Days,levels=c("Monday","Tuesday","Wednesday","Thursd
ay","Friday","Saturday","Sunday"),ordered=TRUE))
df1

The following dataframe is created

  Week_Days  Days_Number
1     Friday          5
2     Monday          1
3    Tuesday          2
4   Saturday          6
5   Thursday          4
6    Tuesday          2
7     Monday          1
8   Thursday          4
9   Thursday          4
10    Friday          5
11  Thursday          4
12    Sunday          7
13  Thursday          4
14   Tuesday          2
15 Wednesday          3
16    Sunday          7
17    Friday          5
18 Wednesday          3
19    Sunday          7
20  Thursday          4

Example 2

Following snippet creates a sample data frame −

Time<-
sample(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),20,replace=TRUE)
df2<-data.frame(Time)
df2

The following dataframe is created

       Time
1    Tuesday
2   Thursday
3     Monday
4    Tuesday
5   Thursday
6     Sunday
7   Saturday
8     Monday
9   Thursday
10  Saturday
11 Wednesday
12    Monday
13 Wednesday
14    Sunday
15    Sunday
16    Sunday
17    Sunday
18 Wednesday
19  Saturday
20    Friday

To convert Time in df2 to number and reading them in a different column on the above created data frame, add the following code to the above snippet −

Time<-
sample(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),20,replace=TRUE)
df2<-data.frame(Time)
df2$Day_Number<-
as.integer(factor(df2$Time,levels=c("Monday","Tuesday","Wednesday","Thursday","
Friday","Saturday","Sunday"),ordered=TRUE))
df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

       Time Day_Number
1    Tuesday        2
2   Thursday        4
3     Monday        1
4    Tuesday        2
5   Thursday        4
6     Sunday        7
7   Saturday        6
8     Monday        1
9   Thursday        4
10  Saturday        6
11 Wednesday        3
12    Monday        1
13 Wednesday        3
14    Sunday        7
15    Sunday        7
16    Sunday        7
17    Sunday        7
18 Wednesday        3
19  Saturday        6
20    Friday        5

Updated on: 01-Nov-2021

859 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements