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

R语言:按ID计算指定日期差值并新增数据列的技术求助

Hey there! Let's work through this R problem together. I'll start by addressing the date difference calculation you need, then we can circle back to the 'Performed' column once you share a bit more about what that should track.

First, let's assume your data looks something like this (I made a sample to demonstrate):

# Sample data to mimic your structure
df <- data.frame(
  ID = c(0, 1, 0, 2, 2, 0, 3),
  Date = as.Date(c("2023-01-01", "2023-01-05", "2023-01-10", "2023-01-15", "2023-01-17", "2023-01-20", "2023-01-25"))
)

Using dplyr (tidyverse) for clean, readable code

I recommend using the dplyr package here—it makes this kind of row-wise and grouping logic straightforward. Here's how to compute the date difference and set up the columns:

library(dplyr)

df_processed <- df %>%
  # Step 1: Capture dates where ID = 0, leave others as NA
  mutate(prev_zero_date = ifelse(ID == 0, Date, NA)) %>%
  # Step 2: Fill those NA values with the most recent previous 0 date
  fill(prev_zero_date, .direction = "down") %>%
  # Step 3: Calculate difference only for non-0 IDs; set 0 IDs to NA
  mutate(Difference = ifelse(ID != 0, Date - prev_zero_date, NA)) %>%
  # Step 4: Example for Performed column (adjust based on your actual need!)
  # I'm assuming this marks if a difference was calculated—tweak as needed
  mutate(Performed = ifelse(ID != 0, TRUE, FALSE))

# Check the result
print(df_processed)

What this does:

  • prev_zero_date: Creates a column that holds the date of the last preceding row with ID = 0 for every row. The fill() function ensures we carry that date forward until the next ID = 0 comes along.
  • Difference: Computes the number of days between the current row's date and the last 0 date—only for rows where ID isn't 0 (you can adjust this if you want 0 rows to show 0 instead of NA).
  • Performed: Since your original request was incomplete, I added a simple example where it flags rows where we calculated a difference. If this needs to track something else (like whether a task was performed), just share more details and I'll adjust the code!

If you prefer base R (no packages needed):

If you don't want to use dplyr, here's a base R approach that does the same thing:

# Base R solution
df$prev_zero_date <- NA
zero_rows <- which(df$ID == 0)

for (i in 1:nrow(df)) {
  # Find the last 0 row before the current one
  last_zero <- max(zero_rows[zero_rows < i])
  df$prev_zero_date[i] <- df$Date[last_zero]
}

# Calculate difference and Performed column
df$Difference <- ifelse(df$ID != 0, df$Date - df$prev_zero_date, NA)
df$Performed <- ifelse(df$ID != 0, TRUE, FALSE)

Quick note: Make sure your Date column is formatted as a proper R Date object first! If it's stored as text, convert it with df$Date <- as.Date(df$Date).

Let me know if you need to adjust the logic (like handling edge cases, different date units, or clarifying what the 'Performed' column should do)!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:50:00