对于变量t,l和c,我有一个数天的重复测量数据集。我试图计算t和l / c峰值之间的天数差异。我已经设法找到了峰值测量的那一天,但我不确定如何将这些全部放在同一条线上以便我可以计算出日差。
Record | day | t | day_tmax | l | day_lmax | c | day_cmax
--------------------------------------------------------------------
1 | 1 | 40.5 | NA | 2 | NA | 1.6 | NA
--------------------------------------------------------------------
1 | 2 | 136.8 | 2 | 2.5 | 2 | 190.6 | NA
--------------------------------------------------------------------
1 | 3 | 102.7 | NA | 1.6 | NA | 375.8 | NA
--------------------------------------------------------------------
1 | 4 | 55.1 | NA | 1.6 | NA | 515.5 | 4
--------------------------------------------------------------------
1 | 5 | 32.3 | NA | 1.7 | NA | 411.2 | NA
--------------------------------------------------------------------
1 | 6 | 27.4 | NA | 1.5 | NA | 235.7 | NA
--------------------------------------------------------------------
1 | 7 | 17.7 | NA | 1.1 | NA | 187.1 | NA
--------------------------------------------------------------------
1 | 8 | 23 | NA | 1.4 | NA | 257 | NA
--------------------------------------------------------------------
1 | 9 | 17.2 | NA | 1.4 | NA | 170.5 | NA
--------------------------------------------------------------------
1 | 10 | 12.1 | NA | 0.6 | NA | 162.1 | NA
--------------------------------------------------------------------
2 | 1 | 72.2 | NA | 0.9 | 1 | 2.4 | NA
--------------------------------------------------------------------
2 | 2 | 54.3 | NA | 0.8 | na | 3.9 | NA
--------------------------------------------------------------------
2 | 3 | 64.4 | NA | 0.7 | NA | 4.6 | 3
--------------------------------------------------------------------
2 | 4 | 63.1 | NA | 0.5 | NA | 3.1 | NA
--------------------------------------------------------------------
2 | 5 | 65.2 | NA | 0.3 | NA | 4.5 | NA
--------------------------------------------------------------------
2 | 6 | 66.5 | NA | 0.2 | NA | 4.6 | 6
--------------------------------------------------------------------
2 | 7 | 81.5 | 7 | 0.2 | NA | 3.5 | NA
--------------------------------------------------------------------
我知道dplyr:
df1 <- df %>% group_by(record) %>% mutate(day_tmax = nth(day_tmax,2))
但是当原始变量的位置变化时,有没有办法做到这一点?
答案 0 :(得分:2)
你可以这样做:
df%>%
group_by(Record)%>%
mutate(diff_peak_t_lc=day[which.max(t)]-day[which.max(l/c)])
day[which.max(t)]
为day
提供t
最高点,day[which.max(l/c)]
为day
提供l/c
为{{1}}在它的巅峰时期。
编辑:遵循Paul Hiemstra的建议。