Create a date column in R using a date vector excluding weekends.

R ProgrammingServer Side ProgrammingProgramming

If we have a vector of dates that contains all days in a week then we can use that vector to create a date column by excluding weekends with the help of subsetting the vector as shown in the below Examples. After subsetting the vector, we will just need to pass the vector in data.frame function.

Example 1

Consider the below vector of dates −

x<-seq(as.Date("2021-07-06"),as.Date("2021-07-25"),by = 1)

To subset the x and to extract only the weekdays on the above created data frame, add the following code to the above snippet −

x<-seq(as.Date("2021-07-06"),as.Date("2021-07-25"),by = 1)
x<-x[!weekdays(x) %in% c("Saturday","Sunday")]

To create a date column of x on the above created data frame, add the following code to the above snippet −

x<-seq(as.Date("2021-07-06"),as.Date("2021-07-25"),by = 1)
x<-x[!weekdays(x) %in% c("Saturday","Sunday")]
df1<-data.frame(x)
df1

Output

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

        x
1  2021-07-06
2  2021-07-07
3  2021-07-08
4  2021-07-09
5  2021-07-12
6  2021-07-13
7  2021-07-14
8  2021-07-15
9  2021-07-16
10 2021-07-19
11 2021-07-20
12 2021-07-21
13 2021-07-22
14 2021-07-23

Example 2

Consider the below vector of dates −

y<-seq(as.Date("2021-01-01"),as.Date("2021-01-31"),by = 1)

To subset the x and to extract only the weekdays on the above created data frame, add the following code to the above snippet −

y<-seq(as.Date("2021-01-01"),as.Date("2021-01-31"),by = 1)
y<-y[!weekdays(y) %in% c("Saturday","Sunday")]

To create a date column of y on the above created data frame, add the following code to the above snippet −

y<-seq(as.Date("2021-01-01"),as.Date("2021-01-31"),by = 1)
y<-y[!weekdays(y) %in% c("Saturday","Sunday")]
df2<-data.frame(y)
df2

Output

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

       y
1  2021-01-01
2  2021-01-04
3  2021-01-05
4  2021-01-06
5  2021-01-07
6  2021-01-08
7  2021-01-11
8  2021-01-12
9  2021-01-13
10 2021-01-14
11 2021-01-15
12 2021-01-18
13 2021-01-19
14 2021-01-20
15 2021-01-21
16 2021-01-22
17 2021-01-25
18 2021-01-26
19 2021-01-27
20 2021-01-28
21 2021-01-29

Example 3

Consider the below vector of dates −

z<-seq(as.Date("2021-04-01"),as.Date("2021-04-30"),by=1)

To subset the x and extract only the weekdays on the above created data frame, add the following code to the above snippet −

z<-seq(as.Date("2021-04-01"),as.Date("2021-04-30"),by=1)
z<-z[!weekdays(z) %in% c("Saturday","Sunday")]

To create a date column of z on the above created data frame, add the following code to the above snippet −

z<-seq(as.Date("2021-04-01"),as.Date("2021-04-30"),by=1)
z<-z[!weekdays(z) %in% c("Saturday","Sunday")]
df3<-data.frame(z)
df3

Output

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

       z
1  2021-04-01
2  2021-04-02
3  2021-04-05
4  2021-04-06
5  2021-04-07
6  2021-04-08
7  2021-04-09
8  2021-04-12
9  2021-04-13
10 2021-04-14
11 2021-04-15
12 2021-04-16
13 2021-04-19
14 2021-04-20
15 2021-04-21
16 2021-04-22
17 2021-04-23
18 2021-04-26
19 2021-04-27
20 2021-04-28
21 2021-04-29
22 2021-04-30
raja
Updated on 28-Oct-2021 07:33:11

Advertisements