我想填充这样的一个系列。R根据以前的值填充列
My result ACTUAL Expected
FWK_SEQ_NBR a initial_d initial_c b c d b c d
914 9.161 131 62 0 62 69 0 62 69
915 9.087 131 0 0 53 78 0 53 78
916 8.772 131 0 0 44 140 0 44 87
917 8.698 131 0 0 0 140 0 35 96
918 7.985 131 0 69 52 139 69 96 35
919 6.985 131 0 78 63 138 78 168 0
920 7.077 131 0 140 126 138 87 247 0
921 6.651 131 0 140 126 138 96 336 0
922 6.707 131 0 139 125 138 35 364 0
逻辑
a given
b lag of d by 4
c initial c for first week thereafter (c previous row + b current - a current)
d initial d - c current
下面是我用
DS1 = DS %>%
mutate(c = ifelse(FWK_SEQ_NBR == min(FWK_SEQ_NBR), intial_c, 0) ) %>%
mutate(c = lag(c) + b - a)) %>%
mutate(d = initial_d - c) %>%
mutate(d = ifelse(d<0,0,d)) %>%
mutate(b = shift(d, n=4, fill=0, type="lag"))
我没有得到的C右边的代码,你知道我缺少什么。我还附上了实际和预期产出的形象。感谢您的帮助!
Actual and Expected values Image
二图片 - 高附加值产品和商店列
Image - Product and Store as the first two columns- please help
列表下面是实际的代码,我也复制了预期与实际输出的图像。谢谢!
很难理解你在做什么。请按照适用情况编辑提供'a','b','c','d'的样本值,然后输出您想要的结果。 – Drj
现在更有意义。谢谢 – Drj
您确定,您列出的问题和操作顺序是正确的吗?你从'd',''''''''从'b'和'a'推导'b',其中'b'依赖于'd',然后再''从'c'推导。这会造成递归计算问题。 – Drj