递归函数,它在自己的前一个输出上运行

时间:2016-05-28 13:10:27

标签: r loops recursion matrix dataframe

我有特定基准年的价格(在这种情况下是1993年),以及所有年份的倍增因子。使用这些已知的乘数因子,我想计算(基准年)之前和之前所有年份的(项目)价格。

这是输入数据:

Year    City    MultiplicationFactor    Price_BaselineYear
1990    New York          NA            NA
1991    New York          0.9           NA
1992    New York          2.0           NA
1993    New York          0.8           100
1994    New York          0.6           NA
1995    New York          0.8           NA
1996    New York          2.0           NA
1990    Boston             NA           NA
1991    Boston             1.6          NA
1992    Boston             1.25         NA
1993    Boston             0.5          200
1994    Boston             1.75         NA
1995    Boston             2.5          NA
1996    Boston             0.5          NA

构造输入数据的代码:

myData<-structure(list(Year = c(1990L, 1991L, 1992L, 1993L, 1994L, 1995L,1996L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L), City = structure(c(2L,2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Boston","New York"), class = "factor"), MultiplicationFactor = c(NA,0.9, 2, 0.8, 0.6, 0.8, 2, NA, 1.6, 1.25, 0.5, 1.75, 2.5, 0.5),`Price(BaselineYear)` = c(NA, NA, NA, 100L, NA, NA, NA, NA,NA, NA, 200L, NA, NA, NA)), .Names = c("Year", "City", "MultiplicationFactor","Price_BaselineYear"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L))

我想要的输出(最后一列,Price_AllYears):

Year    City    MultiplicationFactor    Price_BaselineYear  Price_AllYears
1990    New York    NA                  NA                  69.4
1991    New York    0.9                 NA                  62.5
1992    New York    2.0                 NA                  125.0
1993    New York    0.8                 100                 100.0
1994    New York    0.6                 NA                  60.0
1995    New York    0.8                 NA                  48.0
1996    New York    2.0                 NA                  96.0
1990    Boston      NA                  NA                  200.0
1991    Boston      1.6                 NA                  320.0
1992    Boston      1.25                NA                  400.0
1993    Boston      0.5                 200                 200.0
1994    Boston      1.75                NA                  350.0
1995    Boston      2.5                 NA                  875.0
1996    Boston      0.5                 NA                  437.5

到目前为止,感谢@alistaire:

myData %>%
  group_by(City) %>%
  arrange(Year) %>%
  mutate(Price_AllYears = ifelse(Year < Year[which(!is.na(Price_BaselineYear))], 
                        lead(Price_AllYears) / lead(MultiplicationFactor),
                        ifelse(Year > Year[which(!is.na(Price_BaselineYear))],
                               lag(Price_AllYears) * MultiplicationFactor,
                               Price_BaselineYear)))%>%
  ungroup() %>% 
  arrange(City)

这是我得到的错误:

  

错误:找不到对象'Price_AllYears'

如果必须使用Excel,我将使用以下方法:

    A       B       C                       D                   E
1   Year    City    MultiplicationFactor    Price_BaselineYear  Price_AllYears
2   1990    New York    NA                  NA                  E3/C3
3   1991    New York    0.9                 NA                  E4/C4
4   1992    New York    2.0                 NA                  E5/C5
5   1993    New York    0.8                 100                 D5
6   1994    New York    0.6                 NA                  E5*C6
7   1995    New York    0.8                 NA                  E6*C7
8   1996    New York    2.0                 NA                  E7*C8
9   1990    Boston      NA                  NA                  E10/C10
10  1991    Boston      1.6                 NA                  E11/C11
11  1992    Boston      1.25                NA                  E12/C12
12  1993    Boston      0.5                 200                 D12
13  1994    Boston      1.75                NA                  E12*C13
14  1995    Boston      2.5                 NA                  E13*C14
15  1996    Boston      0.5                 NA                  E14*C15

1 个答案:

答案 0 :(得分:1)

a1 = 15 while a1 < 91: ... a1 += 1 向这些行输入一组行号,子集fun,并确定基值MyData的索引。首先检查是否有一个基准价格,如果没有返回NA;否则,计算基数ix.base之前和基数hd之后的乘数。对于其中的每一个,我们可以使用tl来避免问题中的电子表格公式中显示的迭代计算类型。最后将计算的乘数乘以基本价格。使用cumprod将其应用于每个城市。没有使用包裹:

ave

,并提供:

fun <- function(ix) with(MyData[ix, ], {
  ix.base <- which(!is.na(Price_BaselineYear))
  if (length(ix.base) != 1) return(NA)
  hd <- rev(cumprod(rev(1/head(MultiplicationFactor, ix.base)[-1])))
  tl <- cumprod(tail(MultiplicationFactor, - ix.base))
  Price_BaselineYear[ix.base] * c(hd, 1, tl)
})
transform(MyData, Price_AllYears = ave(seq_along(Year), City, FUN = fun))