R 什么';这个密码怎么了?我必须加2才能找到原来的答案?

R 什么';这个密码怎么了?我必须加2才能找到原来的答案?,r,for-loop,statistics,append,finance,R,For Loop,Statistics,Append,Finance,我想使用R找出回收期。因此,我遵循python代码并试图将其转换为R。但是,我得到的答案总是比正确答案少2 我尝试添加2,这只是一个简单的hack。但是,主要问题仍然存在 cashInflows <- data.frame( Year = c(1, 2, 3, 4, 5), ProjectA = c(10000, 20000, 30000, 40000, 20000), ProjectB = c(40000, 3

我想使用R找出回收期。因此,我遵循python代码并试图将其转换为R。但是,我得到的答案总是比正确答案少2

我尝试添加2,这只是一个简单的hack。但是,主要问题仍然存在

cashInflows     <-  data.frame( 
    Year        =   c(1, 2, 3, 4, 5), 
    ProjectA    =   c(10000, 20000, 30000, 40000, 20000), 
    ProjectB    =   c(40000, 30000, 20000, 10000, 20000)
    ) 
total       =   0
years       =   0
cumulative  <-  c()

for (f in cashInflows$ProjectB){
    total       =   f + total
    if (total   < cashOutflows){
        years = years + 1
    cumulative <-   append(cumulative, total)
   }
}

A  <- years - 1
B  <- cashOutflows - cumulative[years]
C  <- cashInflows$ProjectB[years + 2] # Why +2 ???????? ( I don't know specifically yet.)
print (A + (B/C))

cashInflows这里是一些经过一些修改的工作代码

1) 正如其他人所指出的,您需要在某个地方定义
现金流出
,以便运行此功能

2) R使用基于1的索引,因此如果有代码访问向量的第0个索引,您将无法获得预期的结果。我将
years
改为以1开头,这样,如果从未调用递增循环(即,我们在第1年内通过盈亏平衡),我们将得到一个有效的数字(0),而不是NULL

我将代码放在一个名为
test
的函数中,该函数将
现金流出
作为输入

total       =   0
years       =   1
cumulative  <-  0

test <- function(cashOutflows) {
  for (f in cashInflows$ProjectB){
    total       =   f + total
    if (total   < cashOutflows){
      years = years + 1
      cumulative <-   append(cumulative, total)
    }
  }

  A  <- years - 1
  B  <- cashOutflows - cumulative[years]
  C  <- cashInflows$ProjectB[years] 
  return (A + (B/C))
}
这告诉我们,当我们将20k输入到
测试中时,我们得到0.5,因为这是第一年40k流入量的一半。当我们在函数中输入39.999k时,我们的盈亏平衡时间为0.9999年,因为我们不需要整整一年的40k来实现盈亏平衡


可以说,下面的方法对于R更为惯用,我们使用了内置的向量化函数,如
cumsum

cashOutflows <- 70000
output <- cashInflows
output$cuml = cumsum(output$ProjectB)       # Add column with cumulative total
output$excess = output$cuml - cashOutflows  # Calc "excess" over breakeven
output <- subset(output, excess >= 0)[1, ]  # Select first year at or beyond breakeven
output$Breakeven = output$Year - output$excess/output$ProjectB
output$Breakeven

现金流出这里是一些经过一些修改的工作代码

1) 正如其他人所指出的,您需要在某个地方定义
现金流出
,以便运行此功能

2) R使用基于1的索引,因此如果有代码访问向量的第0个索引,您将无法获得预期的结果。我将
years
改为以1开头,这样,如果从未调用递增循环(即,我们在第1年内通过盈亏平衡),我们将得到一个有效的数字(0),而不是NULL

我将代码放在一个名为
test
的函数中,该函数将
现金流出
作为输入

total       =   0
years       =   1
cumulative  <-  0

test <- function(cashOutflows) {
  for (f in cashInflows$ProjectB){
    total       =   f + total
    if (total   < cashOutflows){
      years = years + 1
      cumulative <-   append(cumulative, total)
    }
  }

  A  <- years - 1
  B  <- cashOutflows - cumulative[years]
  C  <- cashInflows$ProjectB[years] 
  return (A + (B/C))
}
这告诉我们,当我们将20k输入到
测试中时,我们得到0.5,因为这是第一年40k流入量的一半。当我们在函数中输入39.999k时,我们的盈亏平衡时间为0.9999年,因为我们不需要整整一年的40k来实现盈亏平衡


可以说,下面的方法对于R更为惯用,我们使用了内置的向量化函数,如
cumsum

cashOutflows <- 70000
output <- cashInflows
output$cuml = cumsum(output$ProjectB)       # Add column with cumulative total
output$excess = output$cuml - cashOutflows  # Calc "excess" over breakeven
output <- subset(output, excess >= 0)[1, ]  # Select first year at or beyond breakeven
output$Breakeven = output$Year - output$excess/output$ProjectB
output$Breakeven

现金流出转换后数据应该是什么样子?
现金流出错误:未找到对象“现金流出”
。您尚未在for循环语句中定义现金流出。转换后数据应该是什么样子?
现金流出错误:未找到对象“现金流出”
。您尚未定义现金流出循环报表中的现金流出。