- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a date column in R using a date vector excluding weekends.
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
- Related Articles
- How to create a date vector with randomization in R?
- How to add days to date including or excluding weekends and holidays in Excel
- How to convert a date or date vector to POSIXct in R?
- How to create a table with date column?
- Create a sample of data frame column by excluding NAs in R
- How to create a sequence of dates by using starting date in R?
- How to subset a data frame by excluding the column names that are stored in a vector in R?
- How to create a date picker using JavaFX?
- How to subset a data frame by excluding a column using dplyr in R?
- Update MySQL table column by matching date using date() function?
- How to Autofill Weekdays Excluding Weekends in a List of Google Sheet?
- Create a Date object using the Calendar class in Java
- How do you create a date object from a date in Swift xcode?
- How to find the mean of a column grouped by date in R?
- How to create a sequence of time in minutes with date in R?
