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

在R中自动化实现每日CSV数据追加并添加当前日期

Got it, let's break down how to solve this step by step—you're looking to automate daily CSV appending with a date stamp, and I'll walk you through a robust R-based solution that fits your workflow.

Step-by-Step Solution for Automated Daily CSV Appending with Date Stamps

First, let's refine the core logic you started with, then add the automation layer to make this hands-off.

1. Core R Script to Process Daily Data

This script will handle reading your daily CSV, adding the current date automatically, and appending it to a persistent master dataset. We'll use lubridate for reliable date handling (no more hardcoding dates like march8!).

First, install the required package if you haven't already (run this once):

install.packages("lubridate")

Save this as daily_data_update.R—this is the script we'll automate later:

library(lubridate)

# Update these paths to match your system
daily_csv_path <- "path/to/your/daily_generated.csv" # Location of your daily CSV
master_data_path <- "path/to/master_data.RData" # Where we'll store accumulated data

# 1. Read today's CSV and standardize column names
daily_df <- read.csv(daily_csv_path, stringsAsFactors = FALSE)
colnames(daily_df) <- c("Name", "Number") # Match your expected column names

# 2. Add automatic date stamp (uses system date, no manual input needed)
daily_df$Date <- today() # Creates a proper Date type, not a plain string

# 3. Append to master dataset
if (file.exists(master_data_path)) {
  # Load existing master data if it exists
  load(master_data_path)
  master_df <- rbind(master_df, daily_df)
} else {
  # Start fresh with today's data if no master exists yet
  master_df <- daily_df
}

# 4. Save the updated master dataset
save(master_df, file = master_data_path)

# Optional: Export to CSV for easy human-readable access
write.csv(master_df, "path/to/master_data.csv", row.names = FALSE)

Quick Note on Your Original Approach

Your cbind(df, Date = march8) works, but using lubridate::today() removes the need to manually define a date variable every day—critical for automation. Also, saving the master dataset to a file (instead of just keeping it in a variable) ensures your historical data persists between R sessions.

2. Automate the Daily Task

Now let's set this script to run automatically every day, right after your daily CSV is generated.

Windows: Task Scheduler

  • Open Task Scheduler > Click "Create Basic Task"
  • Name it something like "Daily CSV Data Update"
  • Set the trigger to "Daily" at the time your CSV is ready
  • For the action, select "Start a program"
  • In "Program/script", browse to your Rscript.exe location (usually C:\Program Files\R\R-x.x.x\bin\x64\Rscript.exe)
  • In "Add arguments", paste the full path to your daily_data_update.R (e.g., "C:\Scripts\daily_data_update.R")
  • Finish the wizard, and test it to confirm it runs correctly

Linux/macOS: Cron Job

  • Open your terminal and type crontab -e to edit your cron schedule
  • Add a line to run the script at your desired time. For example, to run at 9 AM daily:
    0 9 * * * /usr/bin/Rscript /path/to/your/daily_data_update.R
    
  • Save and exit—cron will handle running the script automatically each day.

Example Workflow Walkthrough

Using your sample initial data:

Name Number
a 1
b 2
c 3
d 4
e 5
f 6

  • On March 8, the script reads this CSV, adds a Date column with 2024-03-08, and saves it as the master dataset.
  • On March 9, when the new daily CSV arrives (e.g., rows g 7, h 8), the script loads the master data, appends the new rows with 2024-03-09, and saves the updated master.

This keeps all historical data in one place, with clear date stamps for each entry.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:22:14