小编use*_*293的帖子

R中的因子记忆化

我写了这个函数来找到一个阶乘数

fact <- function(n) {
    if (n < 0){
      cat ("Sorry, factorial does not exist for negative numbers", "\n")
    } else if (n == 0){
      cat ("The factorial of 0 is 1", "\n")
    } else {
    results = 1
    for (i in 1:n){
      results = results * i
    }
    cat(paste("The factorial of", n ,"is", results, "\n"))
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想在R中实现Memoization.我在R上有基本思想并尝试使用它们.但我不确定这是前进的方向.你能否也请详细说明这个话题.预先感谢.记忆因素

    fact_tbl <- c(0, 1, rep(NA, 100))
    fact_mem <- function(n){
          stopifnot(n > 0)
          if(!is.na(fib_tbl[n])){
           fib_tbl[n]
    } else {
       fact_tbl[n-1] <<- fac_mem(n-1) * …
Run Code Online (Sandbox Code Playgroud)

r memoization factorial

2
推荐指数
1
解决办法
1649
查看次数

在 R 中将数据帧转换为时间序列

我有过去两年半的月度数据。我想将我的数据框转换为时间序列。所以我应该有

Start :: 2015-01-01

End :: 2017-06-01

Frequency : 1
Run Code Online (Sandbox Code Playgroud)

我努力了

ts (df [, -1], start = df [1, 1], end = df [29, 1])
Run Code Online (Sandbox Code Playgroud)

但我从中得到了真正的有线输出。

    Time Series:
    Start = 16436 
    End = 17287 
   Frequency = 1 

            date inflow
   1  2015-01-01   6434

   2  2015-02-01   5595

   3  2015-03-01   3101

   4  2015-04-01   3475

   5  2015-05-01   6519

   6  2015-06-01   7251

   7  2015-07-01   4200

   8  2015-08-01   3622

   9  2015-09-01   4782

   10 2015-10-01   6503

   11 2015-11-01   9460

   12 2015-12-01  15623

   13 2016-01-01  18393

   14 2016-02-01 …
Run Code Online (Sandbox Code Playgroud)

statistics r time-series

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

r ×2

factorial ×1

memoization ×1

statistics ×1

time-series ×1