You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于周末日期列在周度数据中创建节假日虚拟变量(附R示例)

Creating a Holiday Dummy Variable for Weekly Data with Weekend Dates

Got it, let's walk through how to add a holiday dummy variable to your weekly dataset where the Date column is tied to weekend (in your sample, it's weekly Fridays—close enough!) dates. Here's a straightforward approach using R:

Step 1: Start with Your Existing Data

First, let's confirm your dataset setup (you already have this, but just to align):

df <- data.frame(Date=seq(as.Date("2014-01-03"), as.Date("2020-12-25"), by="week"))
df$week <- seq(nrow(df))
df <- df[, c("week", "Date")]

Step 2: Define Your Holiday List

You have two options here—use a pre-built list of holidays (like NYSE trading holidays) or define your own custom list.

Option A: Use Pre-Built Holidays (with timeDate Package)

This is great if you're working with standard U.S. holidays. Install and load the timeDate package to pull official holidays:

# Install if you haven't already
# install.packages("timeDate")
library(timeDate)

# Get NYSE holidays for 2014-2020, convert to Date type
holidays <- as.Date(holidayNYSE(year = 2014:2020))

Option B: Create a Custom Holiday List

If you need specific holidays (like regional or company-specific ones), define them manually:

custom_holidays <- as.Date(c(
  "2014-01-01", "2014-01-20", "2014-02-17", "2014-05-26", "2014-07-04",
  "2014-09-01", "2014-11-27", "2014-12-25",
  "2015-01-01", "2015-01-19", # Add all 2015-2020 holidays here
  "2020-12-25"
))
# Assign to holidays variable to keep consistent code below
holidays <- custom_holidays

Step 3: Add the Holiday Dummy Variable

Now choose the logic that fits your needs:

Case 1: Dummy = 1 if Date is a Holiday

If you only care if the specific weekend/Friday date in your dataset is a holiday:

df$holiday_dummy <- ifelse(df$Date %in% holidays, 1, 0)

# Check the tail of your data—notice 2020-12-25 (Christmas) is marked 1!
tail(df)

Case 2: Dummy = 1 if the Week Contains Any Holiday

If you want to flag weeks that have any holiday (even if the holiday isn't on your Date column's day), use the lubridate package to define the week range:

# Install if needed
# install.packages("lubridate")
library(lubridate)

# Calculate the start (Monday) and end (Sunday) of each week
df$week_start <- df$Date - wday(df$Date, week_start = 1) + 1
df$week_end <- df$week_start + 6

# Check if any holiday falls within the week's range
df$week_has_holiday <- sapply(1:nrow(df), function(i) {
  any(holidays >= df$week_start[i] & holidays <= df$week_end[i])
})
# Convert to 1/0 instead of TRUE/FALSE if needed
df$week_has_holiday <- as.integer(df$week_has_holiday)

Example Output

For the tail of your dataset, you'll see something like this (for Case 1):

tail(df)
#>    week       Date holiday_dummy
#> 360  360 2020-11-20             0
#> 361  361 2020-11-27             1  # Thanksgiving Friday
#> 362  362 2020-12-04             0
#> 363  363 2020-12-11             0
#> 364  364 2020-12-18             0
#> 365  365 2020-12-25             1  # Christmas

内容的提问来源于stack exchange,提问作者khelytics

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:41:57