【问题标题】:How can I make the values ​of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time我怎样才能使这个数据框的值逐渐添加。前任。第二个等于第一个+第二个,依此类推,作为连续时间
【发布时间】:2020-02-22 14:14:03
【问题描述】:

我的代码是这样写的:

HoraLLamadaSimulado2 <- function(n = 50)
{
  tiempodellamada <- 0
  x <- 1
  HORA <- 0
  vec1 <- data.frame()
  while (x < n)
  {
    tiempoentrellamada <- (runif(n, 1000, 1010))
    LLX <- tiempoentrellamada
    HORA <- as.POSIXct(LLX, origin = "2019-01-01", tz = "GMT")
    #HORA <- HORA + tiempoentrellamada
    vec1 <- data.frame(HORA)
    x <- x + 1
  }
  return(vec1)
}
HoraLLamadaSimulado2()

这就是我得到的。那是均匀分布的50倍。我无法计算出连续时间的总和。

霍拉 2019-01-01 00:16:46
2019-01-01 00:16:46
2019-01-01 00:16:43
2019-01-01 00:16:47
2019-01-01 00:16:47
2019-01-01 00:16:47
2019-01-01 00:16:47
2019-01-01 00:16:48
2019-01-01 00:16:48
2019-01-01 00:16:43

【问题讨论】:

  • 从问题标题来看,我认为cumsum 将是一个有用的功能,但从您的代码和简短的输出来看,您还不清楚您需要发生什么。也许你可以解释你最终需要什么?举个很好的例子,从set.seed(2)(或其他东西)开始并使用(比如)3 的n,然后显示您期望的输出。
  • 我需要的是创建一个遵循统一分布的日期时间数据框,它可以为您提供下一个日期时间之间的时间。但在我的数据框中,它只是给出之间的时间,而不是与前一个的总和。

标签: r dataframe rstudio


【解决方案1】:

cumsum的使用怎么样:

set.seed(42)
as.POSIXct(cumsum(runif(10, 1000, 1010)), origin = "2019-01-01")
#  [1] "2018-12-31 16:16:49 PST" "2018-12-31 16:33:38 PST"
#  [3] "2018-12-31 16:50:21 PST" "2018-12-31 17:07:09 PST"
#  [5] "2018-12-31 17:23:56 PST" "2018-12-31 17:40:41 PST"
#  [7] "2018-12-31 17:57:28 PST" "2018-12-31 18:14:10 PST"
#  [9] "2018-12-31 18:30:56 PST" "2018-12-31 18:47:43 PST"

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多