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

在R语言中检查日期列表是否在指定范围及数据集整合

Checking Date Range Membership in R for Squirrel Demographic Data

Alright, let's walk through how to verify if dates from your second dataset fall within each squirrel's birth-to-death window using your existing core data. First, let's recap your core dataset stats for context:

summary(complete)
squirrel_id byear dyear
Min. : 416 Min. :1989 Min. :1989
1st Qu.: 4152 1st Qu.:1997 1st Qu.:1998
Median : 7870 Median :2003 Median :2004
Mean :10419 Mean :2004 Mean :2004
3rd Qu.:16126 3rd Qu.:2011 3rd Qu.:2012
Max. :23327 Max. :2017 Max. :2017

Step 1: Align Your Datasets

First, you'll need to link your second dataset (let's call it new_data) to the core complete dataset using the shared squirrel_id. This ensures each date in new_data is paired with the correct birth and death year for that squirrel.

Option A: Using Base R

If you prefer base R, use merge():

# Assume new_data has columns: squirrel_id, check_year (the date you want to verify)
merged_data <- merge(new_data, complete, by = "squirrel_id", all.x = TRUE)

The all.x = TRUE keeps all rows from your new dataset even if there's no matching squirrel in the core data (adjust this if you only want to keep rows with valid squirrel matches).

Option B: Using Tidyverse (dplyr)

For a more readable, pipe-based workflow:

library(dplyr)

merged_data <- new_data %>%
  left_join(complete, by = "squirrel_id")

Step 2: Add the Range Check Column

Now, add a logical column to flag whether your target date falls within the squirrel's birth-to-death range.

For Year-Only Data (Matching Your Core Dataset)

If your target date is a numeric year (same format as byear/dyear):

# Base R
merged_data$is_in_range <- with(merged_data, check_year >= byear & check_year <= dyear)

# Tidyverse
merged_data <- merged_data %>%
  mutate(is_in_range = check_year >= byear & check_year <= dyear)

For Full Date Values (If Your Check Date Is a Date Object)

If your target is a full date (e.g., "2005-06-15"), convert byear and dyear to Date objects first for accurate comparison:

# Base R
merged_data$birth_date <- as.Date(paste0(merged_data$byear, "-01-01"))
merged_data$death_date <- as.Date(paste0(merged_data$dyear, "-12-31"))
merged_data$is_in_range <- with(merged_data, check_date >= birth_date & check_date <= death_date)

# Tidyverse
merged_data <- merged_data %>%
  mutate(
    birth_date = as.Date(paste0(byear, "-01-01")),
    death_date = as.Date(paste0(dyear, "-12-31")),
    is_in_range = check_date >= birth_date & check_date <= death_date
  )

Step 3: Handle Edge Cases

  • Missing Values: If some entries have NA for byear/dyear or the check date, the is_in_range column will return NA. You can replace these with FALSE if needed:
    # Base R
    merged_data$is_in_range <- ifelse(is.na(merged_data$is_in_range), FALSE, merged_data$is_in_range)
    
    # Tidyverse
    merged_data <- merged_data %>%
      mutate(is_in_range = replace_na(is_in_range, FALSE))
    
  • Exact Matches: The code above includes the birth and death years/dates (using >= and <=). If you want to exclude exact birth/death boundaries, switch to > and <.

That's it! You'll now have a clear flag for each entry in your second dataset indicating whether it falls within the relevant squirrel's lifespan.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:14:08