我想我可能会比我能咀嚼更多。我试图基于几个因素将逻辑方程应用于一组数据,以便为每年制定最终估计。我把所有数据放在同一个地方,但现在我正在努力适当地操纵它。
长期以来我有一个如下所示的数据框:
YEAR ID V1 V2 V3 Delta1 Delta2
1990 A 3 NA NA NA NA
1991 A 5 2 NA 2 NA
1992 A 7 4 6 2 2
1990 B 3 1 NA NA NA
1991 B 5 2 NA 2 1
1992 B 7 1 NA 2 -1
etc
我想应用以下逻辑来计算每一行的新列:
1990年的每个身份证件
if there is a V3 value that will be selected
else if
if there is a V2 value that will be selected for the new column
else
they are assigned the value of V1 (V1 is always populated).
对于每个进行年度,ID都会根据
分配一个值if there is a V3 value it equals V3 * Delta1
else if
the ID has never had a V3 value the calculated value will equal V2
else if
it has had a V3 but just not this year it equals the years previous calculated value for that ID * Delta2
else
the calculated value simply equals the previous years calculated value * Delta1
我知道如何应用if else逻辑,但是如果针对数据框每个ID迭代多年,我会迷失方向。任何帮助将不胜感激,谢谢。
编辑:
理想情况下,输出看起来像
YEAR ID V1 V2 V3 Delta1 Delta2 CalculateColumn
1990 A 3 NA NA NA NA 3
1991 A 5 2 NA 2 NA 4
1992 A 7 4 6 2 2 6
1990 B 3 1 NA NA NA 1
1991 B 5 2 NA 2 1 2
1992 B 7 1 NA 2 -1 4
etc