在R语言中检查日期列表是否在指定范围及数据集整合
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/dyearor the check date, theis_in_rangecolumn will return NA. You can replace these withFALSEif 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

