长格式数据下的增长率计算咨询:松鼠个体体重增长率求解
Got it, let's break this down for you—whether you're working with your current wide-format squirrel data or switch to long-format, calculating weight growth rates is totally manageable with some tidy data tools. Here's how to do both:
Your current data has one row per squirrel, with repeated columns for dates, ages, and weights (like date1/age1/wt1, date2/age2/wt2, etc.). First, we need to pair up each valid age-weight observation for a squirrel, then compute growth rates.
We'll use R's tidyverse package (standard for ecological data wrangling) to handle this:
# Load required package library(tidyverse) # Assume your data frame is named squirrel_wide growth_wide <- squirrel_wide %>% # Reshape wide columns to pair age/wt for each time point pivot_longer( cols = starts_with(c("age", "wt")), names_to = c(".value", "time_point"), names_pattern = "(age|wt)(\\d+)" ) %>% # Remove rows where age or weight is missing (NA) drop_na(age, wt) %>% # Group by individual squirrel to calculate per-squirrel rates group_by(squirrel) %>% # Sort observations by age to ensure we're using chronological order arrange(age, .by_group = TRUE) %>% # Calculate two common growth metrics: # 1. Percent growth rate between consecutive measurements # 2. Daily weight gain (grams per day) mutate( percent_growth_rate = ((wt - lag(wt)) / lag(wt)) * 100, daily_weight_gain = (wt - lag(wt)) / (age - lag(age)) ) %>% # Optional: Calculate overall average daily growth using linear regression # (this gives a single rate per squirrel, even with multiple time points) mutate(overall_daily_growth = coef(lm(wt ~ age, data = cur_data()))[2]) %>% ungroup()
What this does:
pivot_longertakes your wide columns and turns them into rows where each row is one age-weight measurement for a squirrel.drop_nagets rid of useless rows where we don't have both age and weight data.arrangeensures we're calculating growth in the correct order (from youngest to oldest).- The
mutatesteps compute:- Percent growth rate: How much weight the squirrel gained (as a percentage) between two measurements.
- Daily weight gain: The average grams gained per day between two time points.
- Overall daily growth: The slope of a linear regression of weight vs. age, which gives a single average growth rate per squirrel across all their measurements.
If your data was already in long-format (one row per squirrel per measurement, with columns like squirrel, age, wt, date, etc.), the process is even simpler—you skip the reshaping step entirely.
Example long-format data structure:
| squirrel | fieldBirthDate | date | age | wt | litterid |
|---|---|---|---|---|---|
| 22922 | 2017-05-13 | 2017-05-14 | 1 | 12 | 7684 |
| 22922 | 2017-05-13 | 2017-06-07 | 25 | 52.9 | 7684 |
| 22976 | 2017-05-13 | 2017-05-16 | 3 | 15.5 | 7692 |
Here's the code to compute growth rates for long-format data:
# Assume your long-format data frame is named squirrel_long growth_long <- squirrel_long %>% drop_na(age, wt) %>% group_by(squirrel) %>% arrange(age, .by_group = TRUE) %>% mutate( percent_growth_rate = ((wt - lag(wt)) / lag(wt)) * 100, daily_weight_gain = (wt - lag(wt)) / (age - lag(age)), overall_daily_growth = coef(lm(wt ~ age, data = cur_data()))[2] ) %>% ungroup()
Extra tips:
- If you're expecting exponential growth (common in young animals), use logarithmic growth rate instead:
log(wt) - log(lag(wt))(this gives the continuous growth rate). - Squirrels with only one measurement will have
NAfor the consecutive growth rates—you can filter these out withfilter(!is.na(percent_growth_rate))if needed. - If weight growth isn't linear (e.g., slows down as the squirrel matures), consider fitting a nonlinear model (like a logistic growth curve) instead of linear regression for the overall rate.
内容的提问来源于stack exchange,提问作者Blundering Ecologist

