我确实有 Dataframe ,如下所示:
A <- data.frame(GroupName = c(rep(c("A", "B", "C"), each = 6)),
ObservationName = c("alpha", "beta", "gamma", "alpha", "beta", "gamma", rep(c("delta", "epsilon"),3), rep(c("zeta", "eta", "theta"),2)),
Date = rep(rep(seq(as.Date("2010-1-1"), as.Date("2010-3-1"), by = "month"), each =3), 2),
Value = runif(n = 18, min = 1, max = 10))
现在,我要计算始终在组(组名)内、同一月(日期)内以及该组内所有观测值之间(如果测量在同一月(和组)内)的值的百分比变化。输出结果应如下所示(仅组A的前四种情况,其余情况应如下所示):
B <- data.frame(GroupName = c("A", "A", "A", "A"),
ObservationName1 = c("alpha", "alpha", "beta", "alpha"),
ObservationName2 = c("beta", "gamma", "gamma", "beta"),
Date = c(as.Date("2010-1-1"), as.Date("2010-1-1"), as.Date("2010-1-1"), as.Date("2010-2-1")),
Diff = c(abs(A[1,4]-A[2,4])/ ((A[1,4]+A[2,4])/2), #alpha beta Jan
abs(A[1,4]-A[3,4])/ ((A[1,4]+A[3,4])/2), #alpha gamma Jan
abs(A[2,4]-A[4,4])/ ((A[2,4]+A[4,4])/2), #beta gamma Jan
abs(A[4,4]-A[5,4])/ ((A[4,4]+A[5,4])/2))) #alpha gamma Feb
1条答案
按热度按时间2w3rbyxf1#
使用
combn
:输出
dplyr 1.1.0
之前: