在 r 中按日期排序的数据框上应用方程式

Apply an equation on a dataframe order by date in r

我的数据是这样的:

structure(list(ID = c("h01", "h01", "h01", "h01", "h01"), collection_date = structure(c(15076, 
15076, 15092, 15092, 15125), class = "Date"), wavelength = c(630L, 
800L, 630L, 800L, 630L), R = c(0.078, 0.295, 0.108, 0.361, 0.127
)), row.names = c(NA, 5L), class = "data.frame")

我想做的(但不知道)是应用以下等式: "(wavelength 800 的R - wavelength 630 的R) / (wavelength 800 的R + Rwavelength 630)” 每个人 collection_date 和 return 个人数据帧中的结果。

任何帮助将不胜感激。

我们arrange将数据按'ID'、'collection_date'、'wavelength'分组,然后按'ID'、'collection_date'分组,得到diff 的 'R' 并除以 sum(假设只有 800 和 630 wavelength 存在)。如果只有一个观察值,return NA

library(dplyr)
df1 %>%
     filter(wavelength %in% c(800, 630)) %>% # in case there are other wav
     arrange(ID, collection_date, wavelength) %>%
     group_by(ID, collection_date) %>% 
     summarise(new = if(n() == 1) NA_real_ else diff(R)/sum(R), .groups = 'drop')

-输出

# A tibble: 3 × 3
  ID    collection_date    new
  <chr> <date>           <dbl>
1 h01   2011-04-12       0.582
2 h01   2011-04-28       0.539
3 h01   2011-05-31      NA    

您可以编写一个函数在 match 的帮助下执行计算,并为每个 IDcollection_date.

应用 if
library(dplyr)

result_calc <- function(wave, R) {
  r1 <- R[match(630, wave)]
  r2 <- R[match(800, wave)]
  (r2 - r1)/(r2 + r1)
}

df %>%
  group_by(ID, collection_date) %>%
  summarise(result = result_calc(wavelength, R), .groups = 'drop')

#   ID    collection_date result
#  <chr> <date>           <dbl>
#1 h01   2011-04-12       0.582
#2 h01   2011-04-28       0.539
#3 h01   2011-05-31      NA