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

使用dplyr计算非统一采样频率的月度叶片生长率问题

Handling Monthly Growth Rates with Mixed Sampling Frequencies

Great question! Mixed sampling frequencies (like weekly vs. bi-monthly) can throw a wrench into straightforward lag-based calculations, but your intuition to standardize the data first is spot-on. Here’s a step-by-step solution using dplyr:

1. First: Standardize to Monthly Frequency

The core issue here is that your raw data has uneven row counts per month. To calculate a meaningful month-over-month growth rate, you first need to collapse each month’s data into a single representative value for each plant. You have two common options for this representative value (pick based on your research goals):

  • Monthly average: Smooths out sampling noise if your measurements have variability
  • Final monthly measurement: Better if you want to capture the end-of-month growth state

Code to Standardize the Data

library(dplyr)

# Start with your mixed-frequency dataset
toy_growthrate_with_twist <- data.frame(
  Plant_ID = c("365","365","365","365","365","365","365","365","365","365","365","365"),
  Leaf_length = c(1,2,4, 10, 15, 17, 20, 25, 30, 50, 45, 47),
  Month = c(5,5,5,5,6,6,7,7,8,8,9,9),
  Period = c("T1","T2","T3","T4","T1","T2","T1","T2","T1","T2","T1","T2")
)

# Group by plant and month, then calculate representative values
monthly_standardized <- toy_growthrate_with_twist %>%
  group_by(Plant_ID, Month) %>%
  summarise(
    avg_leaf_length = mean(Leaf_length),  # Monthly average
    final_leaf_length = last(Leaf_length) # Final measurement of the month
  ) %>%
  ungroup()

2. Calculate Monthly Growth Rates

Now that you have one row per month per plant, you can use lag() safely to compare each month to the previous one:

# Arrange data by plant and month, then compute growth rates
monthly_growth <- monthly_standardized %>%
  arrange(Plant_ID, Month) %>%
  mutate(
    # Growth rate using monthly average
    growth_pct_avg = ((avg_leaf_length - lag(avg_leaf_length)) / lag(avg_leaf_length)) * 100,
    # Growth rate using final monthly measurement
    growth_pct_final = ((final_leaf_length - lag(final_leaf_length)) / lag(final_leaf_length)) * 100
  )

# View the result
monthly_growth

Why This Works

  • By aggregating to a monthly frequency first, you eliminate the mismatch in row counts between months (4 rows for May vs. 2 for June, etc.). This ensures lag() always pulls the entire previous month’s representative value, not a random earlier sampling point.
  • Choosing between average or final measurement depends on your study’s context: if leaf growth is steady, final measurement is more reflective of monthly progress; if measurements have day-to-day variation, average will smooth out noise.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 16:18:15